Euler 0032
The Problem:
SurprisinglyPandigital therenumbers are only three numbers that canhave be1-n writtenshown asexactly once.
7254 is unusual because 39 * 186 = 7254, which is pandigital through the sumwhole ofcalculation. fourth powers of their digits: 1634, 8208, 9474 We ignore 1 for the trivial case,lol.
FindWhat is the sum of all the numbersproducts that can begenerate writtenthese aspandigital the sum of fifth powers of their digits.calculations?
Considerations and Approach:
WeSeeing needthat it is 1-9 pandigital, it would be good to findskip anmultiplicand upperand limitmultipliers first,that whichcontain 0s.
Assuming a * b = c, we can doincrement byb adding 9^5 to a sumup until the sumtotal digit length is less> than9, the number of digits.There is no number that is going to fit these conditions above that upper limit.
Thenthen we increment 1a and reset b back to thea. upperWhen limitb is reset and finda allis theseincremented multipleswe can check if total digit length is > 9 and break
The Code:
upper_limitbreak_threshold = 9**51000000000 digit_count#just something comfy to break
cycles = 0
pands = set()
a = 1
#findb = 1
#only break if the upperlen limitof the all the digits is greater than 9 (pigeonhole)
while cycles < break_threshold and len(str(a)) + len(str(b)) + len(str(a*b)) <= 9:
current_str = str(a) + str(b) +str(a*b)
#iterate b until the current string length exceeds 9
while len(str(upper_limit))/digit_countcurrent_str) > 1:
upper_limit +<= 9**59:
digit_countif +len(current_str) == 19:
print(upper_limit,#we digit_count)don't #calculateaccept every0s, numberwe betweenare here and there
digit_fifths = []looking for i1-9
if '0' not in range(2,current_str:
upper_limit):#all current_digit_sumdigits =appear 0once forso jthe insum of counts should be 9 (9 numbers)
if sum([int(current_str.count(x) for x in str(i)]:current_str]) current_digit_sum== 9:
pands.add(a*b)
b += j**51
if current_digit_sumcurrent_str == i:str(a) digit_fifths+ str(b) + str(a*b)
#increment the cycle
a += [i]1
b = a
cycles += 1
print(digit_fifths)pands, cycles)
print(sum(digit_fifths)pands))