Digit cancelling fractions

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.


Idea

The key to this problem is to understand what 'curious fraction', 'trivial example', 'non-trivial example' means.

  • 'curious fraction' means numerator and denominator have common digit, and if remove this common digit, the value will still be same
  • 'trivial example' means both numerator and denominator have 'tail 0', which is obviously a curious fraction
  • 'non-trivial example' is curious fraction but not trivial

In [1]:
from collections import Counter
from functools import reduce
from operator import mul
from math import gcd
In [2]:
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]
In [3]:
get_common_digit(10 ,20)
Out[3]:
[0]
In [4]:
get_common_digit(11, 22)
Out[4]:
[]
In [5]:
get_common_digit(12, 34)
Out[5]:
[]
In [6]:
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
In [7]:
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
In [8]:
solve()
Out[8]:
100