euler/python/e033.py

39 lines
963 B
Python
Raw Normal View History

2019-07-16 18:51:07 +02:00
from lib_misc import gcd
def is_curious(n, d):
assert(len(str(n)) == 2 and len(str(d)) == 2)
if n == d:
return False
for i in range(1, 10):
if str(i) in str(n) and str(i) in str(d):
try:
n_ = int(str(n).replace(str(i), ""))
d_ = int(str(d).replace(str(i), ""))
except ValueError:
return False
try:
if n_ / d_ == n / d:
return True
except ZeroDivisionError:
return False
return False
def euler_033():
fs = [(n, d) for n in range(10, 100)
for d in range(n, 100) if is_curious(n, d)]
n = 1
d = 1
for n_, d_ in fs:
n *= n_
d *= d_
return d // gcd(n, d)
if __name__ == "__main__":
assert(is_curious(49, 98) is True)
assert(is_curious(30, 50) is False)
print("e033.py: {}".format(euler_033()))
assert(euler_033() == 100)