Project Euler Problem 033
Statement
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.
Solution
Just a brute-force, straight-forward solution using Python's fractions library.
from fractions import Fraction def is_weird_fraction(num, denom): main = Fraction(num, denom) simplified = [] if num % 10 == denom % 10: simplified.append(Fraction(num // 10, denom // 10)) if num % 10 == denom // 10: simplified.append(Fraction(num // 10, denom % 10)) if num // 10 == denom % 10: simplified.append(Fraction(num % 10, denom // 10)) if num // 10 == denom // 10: simplified.append(Fraction(num % 10, denom % 10)) for fract in simplified: if main == fract: return True return False if __name__ == '__main__': lst = [] for num in range(10, 100): for denom in range(num+1, 100): if not(num % 10 == 0 or denom % 10 == 0) and is_weird_fraction(num, denom): lst.append(Fraction(num, denom)) result = Fraction(1,1) for f in lst: result *= f print("The result is:", result.denominator)
The Python file is available for download here.