```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 ``` ```#!/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])) ```
 ```1 ``` ```12321 33124 34225 ```