[ create a new paste ] login | about

Link: http://codepad.org/l2hDGwBm    [ raw code | output | fork ]

Python, pasted on Mar 26:
#!/usr/bin/python

from collections import defaultdict
from itertools import count, takewhile

def combinations(seq, m):
    def comb(ls, m):
        if m == 0:
            yield []
        else:
            for i, item in enumerate(ls):
                for rest in comb(ls[i + 1:], m - 1):
                    yield [item] + rest
    return comb(list(seq), m)
        
def histogram(s):
    d = defaultdict(int)
    for c in s:
        d[int(c)] += 1
    return d

squares = filter(lambda s: len(s) == 5 and all(c in '12345' for c in s),
                 takewhile(lambda s: len(s) <= 5,
                           (str(s**2) for s in count())))

matches = defaultdict(list)
for a, b, c in combinations(squares, 3):
    hist = histogram(a + b + c)
    digits = set(hist.keys())
    counts = set(hist.values())
    if    (len(digits) == 5 and
           digits == counts and
           not any(hist[k] == k for k in hist)):
        inverse_hist = dict((hist[k], k) for k in hist)
        singleton = inverse_hist[1]
        matches[singleton].append((a, b, c))

match_counts = dict((len(ls), ls) for ls in matches.values())
print (' '.join(match_counts[1][0]))


Output:
1
12321 33124 34225


Create a new paste based on this one


Comments: