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.


In [1]:
from euler import greatest_common_divisor

In [2]:
greatest_common_divisor(12, 20)


Out[2]:
4

In [3]:
def foo():
    n = set([])
    for aa in range(10, 99+1):
        aas = str(aa)
        for bb in range(aa+1, 99+1):
            bbs = str(bb)
            for ai in range(len(aas)): # ai is index of digit to clobber
                ad = aas[ai]
                a = int(aas[:ai] + aas[ai+1:])
                for bi in range(len(bbs)): # bi is index of digit to clobber
                    bd = bbs[bi]
                    b = int(bbs[:bi] + bbs[bi+1:])
                    if ad == bd and int(ad) and int(bd) and a * bb == b * aa:
                        s = '%d/%d == %d/%d' % (aa, bb, a, b)
                        n.add((aa, bb))
    a = reduce((lambda x, y: x * y), (i[0] for i in n))
    b = reduce((lambda x, y: x * y), (i[1] for i in n))
    return b / greatest_common_divisor(a, b)

In [4]:
%timeit foo()
foo()


10 loops, best of 3: 42 ms per loop
Out[4]:
100