euler/python/e026.py

32 lines
823 B
Python
Raw Normal View History

2019-07-16 04:11:49 +02:00
from itertools import count
2015-11-16 20:56:24 +01:00
2019-07-16 04:11:49 +02:00
def get_cycle_count(nominator, denominator):
assert(nominator == 1)
remainders = {}
remainder = nominator
results = []
for i in count():
result = remainder // denominator
remainder = remainder % denominator
results.append(result)
if remainder in remainders:
return i - remainders[remainder]
else:
remainders[remainder] = i
if remainder == 0:
return 0
remainder *= 10
2015-11-16 20:56:24 +01:00
2019-07-16 04:11:49 +02:00
def euler_026():
return max([(get_cycle_count(1, i), i) for i in range(1, 1000)])[1]
2015-11-16 20:56:24 +01:00
2019-07-16 04:11:49 +02:00
if __name__ == "__main__":
assert(get_cycle_count(1, 7) == 6)
assert(get_cycle_count(1, 10) == 0)
assert(get_cycle_count(1, 6) == 1)
print("e026.py: {}".format(euler_026()))
assert(euler_026() == 983)