(Python) import itertools def ipartition(n,k,l=1): '''n is the integer to partition, k is the length of partitions, l is the min partition element size''' if k < 1: raise StopIteration if k == 1: if n >= l: yield (n,) raise StopIteration for i in range(l,n+1): for result in ipartition(n-i,k-1,i): yield (i,)+result # generate once all the tuples of distinct digits cached_tuple = {} for n in range(1, 11): comb = itertools.combinations([0,1,2,3,4,5,6,7,8,9], n) cached_tuple[n] = [] for c in comb: for p in itertools.permutations(c): cached_tuple[n].append(p) shys = set() for digits in range(3, 9): print "scanning", digits, "digit numbers" precomputed_power = {} for d in range(10, -1, -1): # all digits from 10 to 0 precomputed_power[d] = (10-d)**digits for partsize in range(1, 11): partitions = ipartition(digits, partsize) # if the partition has equal numbers it can be optimized for part in partitions: numdifferentdigits=len(part) for perm in cached_tuple[numdifferentdigits]: permindex=0 sum = 0 formed_num_map = {} index=0 for digit in perm: sum += precomputed_power[digit] * part[index] formed_num_map[digit] = part[index] index+=1 permindex += 1 sum_number_str=str(sum) sum_number_map={} for d in sum_number_str: try: sum_number_map[int(d)] += 1 except KeyError: sum_number_map[int(d)] = 1 if sum_number_map == formed_num_map: if sum not in shys: shys.add(sum) print "shy:", sum # by Marco Cecchi