The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
We 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 value of the denominator.
The key to this problem is to understand what 'curious fraction', 'trivial example', 'non-trivial example' means.
from collections import Counter
from functools import reduce
from operator import mul
from math import gcd
def get_common_digit(a, b):
assert 10 <= a <= 99
assert 10 <= b <= 99
return [int(d1) for d1 in str(a) for d2 in str(b) if d1 == d2]
get_common_digit(10 ,20)
get_common_digit(11, 22)
get_common_digit(12, 34)
def remove_common_digit(a, b):
if a % 10 == 0 and b % 10 == 0:
return None, None
d = get_common_digit(a, b)
if len(d) == 1:
digit = d[0]
return a // 10 if a % 10 == digit else a % 10, b // 10 if b % 10 == digit else b % 10
else:
return None, None
def solve():
fractions = []
for numerator in range(10, 100):
for denominator in range(numerator, 100):
new_numerator, new_denominator = remove_common_digit(numerator, denominator)
if new_numerator and new_denominator and numerator * new_denominator == denominator * new_numerator:
fractions.append((numerator, denominator))
assert len(fractions) == 4
numerators, denominators = zip(*fractions)
p = (reduce(mul, numerators, 1), reduce(mul, denominators, 1))
g = gcd(*p)
return p[1] // g
solve()