Home Project Euler Teaching Projects Gallery

Problem 33: 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.


Algorthim:

There's no real algorthim in this problem but rather come up with a way to directly check if two fractions are equal given that there's only exactly ONE digit in common. So procedure is as follows:

  1. Make two for loops for the numerator and denomiator
  2. Take into account of zero exist in the digits, if it does, it doesn't count as seen by the problem statement
  3. Find the intersection of the digits of numerator and denominator. If the length of the intersection is greater than 1, skip. If it's one, check if by removing the common digit is the same as the original digit
  4. Continue throughout the for loops!

Code:

Result:

Answer: 100
Runtime: 44 ms