Problem 32

Digit cancelling fractions

Problem 34

Digit cancelling fractions

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.
from tools import gcd


def run():
    n_total, d_total = 1, 1
    for d in range(10, 100):
        d2 = d % 10
        if d2 == 0:
            continue
        d1 = d // 10
        for n in range(10, d):
            n2 = n % 10
            if n2 == 0:
                continue
            n1 = n // 10

            ok = False
            if n1 == d1 and n * d2 == d * n2:
                ok = True
            elif n1 == d2 and n * d1 == d * n2:
                ok = True
            elif n2 == d2 and n * d1 == d * n1:
                ok = True
            elif n2 == d1 and n * d2 == d * n1:
                ok = True

            if ok:
                n_total *= n
                d_total *= d

    g = gcd(n_total, d_total)

    return d_total // g