top of page

🔢 Project Euler #33 — when "wrong" maths turns out to be right

  • Brodie Mooy
  • May 12
  • 2 min read

Some fractions look like they simplify by incorrectly cancelling digits — yet give the correct result anyway.


Take 49/98. Cancel the 9s and you get 4/8 = 1/2. And 49/98 = 1/2. âś…

These are called digit-cancelling fractions — and there are exactly 4 non-trivial ones where both numerator and denominator are two-digit numbers.

Project Euler #33 asks you to find them all, multiply them together, and return the denominator of the result in its lowest terms.

My approach in Python: → Brute-force all two-digit pairs where numerator < denominator → Skip trivial cases (any digit is 0) → Check two cancellation patterns: first digits match, or inner digits match → Use Python's Fraction class for exact arithmetic — no floating-point surprises


from fractions import Fraction
candidates = []

for i in range(10,100):
    for j in range(10,100):
        if i < j:
            x = str(i)
            y = str(j)
            a = list(x)
            b = list(y)
            if a[1] == '0' or b[1] == '0':
                continue
            fraction = i/j
            if a[0] == b[0] and int(a[1])/int(b[1]) == fraction:
                candidates.append(str(i) + "_" + str(j))
            if a[1] == b[0] and int(a[0])/int(b[1]) == fraction:
                candidates.append(str(i) + "_" + str(j))

e = candidates[0]
f = candidates[1]
g = candidates[2]
h = candidates[3]

e1 = str(e)
e2 = int(e[:2])
e3 = int(e[-2:])
e4 = Fraction(e2, e3)

f1 = str(f)
f2 = int(f[:2])
f3 = int(f[-2:])
f4 = Fraction(f2, f3)

g1 = str(g)
g2 = int(g[:2])
g3 = int(g[-2:])
g4 = Fraction(g2, g3)

h1 = str(h)
h2 = int(h[:2])
h3 = int(h[-2:])
h4 = Fraction(h2, h3)

print(e4, f4, g4, h4)

d = e4 * f4 * g4 * h4

print(Fraction(d))



The four fractions multiply to give a denominator of 100.

What I love about this problem: it turns a maths "mistake" into a puzzle. The naive approach (just loop and check) is perfectly effective here — sometimes brute force paired with the right abstraction (exact fractions) is all you need.

Working through Project Euler problems is one of my favourite ways to keep the problem-solving muscles sharp. Each one hides a small insight worth finding. đź§ 



 
 
 

Comments


bottom of page