# Euler Problem 33¶

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.

We start write a function which checks if a number is curios and then brute force.

In [21]:

    def is_curious(n, d):
        assert(len(str(n)) == 2 and len(str(d)) == 2)
        if n == d:
            return False
        for i in range(1, 10):
            if str(i) in str(n) and str(i) in str(d):
                try:
                    n_ = int(str(n).replace(str(i), ""))
                    d_ = int(str(d).replace(str(i), ""))
                except ValueError:
                    return False
                try:
                    if n_ / d_ == n / d:
                        return True
                except ZeroDivisionError:
                    return False
        return False

    assert(is_curious(49, 98) == True)
    assert(is_curious(30, 50) == False)

In [25]:

    fs = [(n, d) for n in range(10, 100) for d in range(n, 100) if is_curious(n, d)]
    fs

Out[25]:

    [(16, 64), (19, 95), (26, 65), (49, 98)]

In [32]:

    n = 1
    d = 1
    for n_, d_ in fs:
        n *= n_
        d *= d_

    print("{}/{}".format(n, d))

    387296/38729600

Now we can see that the solution is $100$. But actually it would be nice to calculate the GCD.

In [40]:

    def gcd_euclid(a, b):
        if a == b:
            return a
        elif a > b:
            return gcd_euclid(a - b, b)
        elif a < b:
            return gcd_euclid(a, b - a)

    gcd_nd = gcd_euclid(n, d)

    s = d // gcd_nd
    assert(s == 100)
    s

Out[40]:

    100