1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from itertools import permutations # The first number is less than 5.10^8 so its square root is # less than 22361. Since the leading digit is 1 or 2, the # sequence must be one of 0..4, 1..5 or 2..6. The number is # divisible by 9 so the square root is divisible by 3 and so # is the sum of its digits. The only sequence whose sum is # a multiple of 3 is 1..5 with the possible square roots of # 12xxx, 13xxx, 14xxx, 15xxx and 21xxx. # for each two digit prefix for pre in ('12', '13', '14', '15', '21' ): # permute the remaining three digits for p in permutations(set('12345') - set(pre)): # find the number as a string s = str(int(pre + ''.join(p)) ** 2) # check it has nine non-zero digits if '0' not in s and len(set(s)) == 9: print(s) |