-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem=26.py
More file actions
37 lines (28 loc) · 1017 Bytes
/
problem=26.py
File metadata and controls
37 lines (28 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Reciprocal Cycles - https://projecteuler.net/problem=26
def reciprocal_cycle_length(d):
"""Returns the length of the recurring cycle in the decimal representation of 1/d."""
remainders = {}
value = 1
position = 0
while value != 0:
if value in remainders:
return position - remainders[value]
remainders[value] = position
value *= 10
value %= d
position += 1
return 0
def find_longest_reciprocal_cycle(limit):
"""Finds the value of d < limit for which 1/d contains the longest recurring cycle."""
max_length = 0
result = 0
for d in range(2, limit):
cycle_length = reciprocal_cycle_length(d)
if cycle_length > max_length:
max_length = cycle_length
result = d
return result
if __name__ == "__main__":
limit = 1000
answer = find_longest_reciprocal_cycle(limit)
print(f"The value of d < {limit} for which 1/d contains the longest recurring cycle is: {answer}")