https://projecteuler.net/problem=33
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly belive 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 faction, 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.
ax/xb == a/b
or xa/bx == a/b
; where a
, b
, and x
are digits 0-9.ax/xb
form, a<=x
because of point 2, and when a==x
, x<b
.xa/bx
form,...10^3
possibilities.
In [1]:
import math
def classify(a, b, x):
if a == b:
return None # trivial example
a = float(a)
b = float(b)
x = float(x)
if a > 0 and x > 0 and b > 0:
n = a*10+x
d = x*10+b
if n < d and math.isclose(n/d, a/b):
return 'ax/xb'
if x > 0 and b > 0:
n = x*10+a
d = b*10+x
if n < d and math.isclose(n/d, a/b):
return 'xa/bx'
return None
In [2]:
classify(4, 8, 9)
Out[2]:
In [3]:
classify(3, 5, 0)
In [4]:
import numpy as np
def find_curious_functions():
for a in range(10):
for b in range(10):
for x in range(10):
c = classify(a, b, x)
if c is not None:
print('a={}, b={}, x={}, classification: {}'.format(
a, b, x, c))
if c == 'ax/xb':
yield np.array([a*10+x, x*10+b])
if x == 'xa/bx':
yield np.array([x*10+a, b*10+x])
curious_fractions = list(find_curious_functions())
curious_fractions
Out[4]:
In [5]:
v = np.array([1,1])
for i in curious_fractions:
v *= i
v
Out[5]:
We could use GCD to simplify the fraction to its lowest common terms, but this is quite straightforward. The denominator is exactly 100 times the numerator. Therefore the answer is 100.