Euler 0033
The Problem:
PandigitalThe numbersfraction are49/98 numbersis a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that have 1-n shown exactly once. 7254 is unusual because 39 * 18649/98 = 7254,4/8, which is pandigitalcorrect, throughis obtained by cancelling the whole calculation.9s.
WhatWe shall consider fractions like, 30/50=3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, find the sumvalue of allthe products that can generate these pandigital calculations?denominator.
Considerations and Approach:
Seeing that it is 1-9 pandigital, it would be good to skip multiplicand and multipliers that contain 0s.
Assuming a * b = c,Naively... we can incrementiterate bthrough the denominator up untilto 100, and then iterate through the totalnumerator digit length is > 9, then we increment a and reset b backup to a.the Whendenominator.
To isfind resetthe and a is incrementedsolution we can check ifevery totalnumber such that the denominator and numerator have an equal digit, and that the result of the fraction is the same as the digit lengthreduced fractions.
We skip every multiple of 10 over 10 since numerator would make the reduced single digit fraction = 0, which won't ever be the case of the base fraction, or it will be undefined, which is >problematic.
We 9can andstart breakfrom 11 every time since we can't digit reduce a single digit number.
The Code:
break_thresholdimport math
upper_limit = 1000000000 #just something comfy to break100
cycles = 0
pandsnon_trivial_den = set()[]
anon_trivial_num = 1[]
b#denominator =and 1numerator
#onlyfor breakdenominator in range(11,100):
for numerator in range(11,denominator):
if thedenominator len% of 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(current_str) <= 9:
if len(current_str)10 == 9:0 #weor don'tnumerator accept% 0s, we are looking for 1-9
if '0' not in current_str:
#all digits appear once so the sum of counts should be 9 (9 numbers)
if sum([current_str.count(x) for x in current_str])10 == 9:0:
pands.add(a*b)pass
belse:
+=#iterate 1through current_strthe denominator to find numbers that are equivalent
counter = 0
for den_index in range(len(str(a)denominator))):
+for num_index in range(len(str(b) + str(a*b)
#increment the cycle
a += 1
b = anumerator))):
cycles += 1
print(pands,if cycles)int(str(numerator)[num_index]) == int(str(denominator)[den_index]):
new_num = int(str(numerator)[:num_index] + str(numerator)[(num_index+1):])
new_den = int(str(denominator)[:den_index] + str(denominator)[(den_index+1):])
if numerator/denominator == new_num/new_den:
print(sum(pands)denominator, numerator, new_num, new_den)
non_trivial_den += [new_den]
non_trivial_num += [new_num]
print(cycles, math.prod(non_trivial_den), math.prod(non_trivial_num))