Skip to content

Instantly share code, notes, and snippets.

@voith
Created November 12, 2019 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voith/f63395417b6f6f7d8b294635cb3233ca to your computer and use it in GitHub Desktop.
Save voith/f63395417b6f6f7d8b294635cb3233ca to your computer and use it in GitHub Desktop.
value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction par
largest_num = 1000
largest_divisor = 3
longest_cycle = 1
rdigits = ''
def get_divide_result(i):
seen_remainders = [False] * i
remainder = 10 % i
cycle_count = 0
while remainder!= 0 and seen_remainders[remainder] is False:
seen_remainders[remainder] = True
cycle_count += 1
remainder = (remainder * 10) % i
return remainder, cycle_count
for i in range(4, largest_num + 1):
remainder, cycle_count = get_divide_result(i)
if remainder != 0 and cycle_count > longest_cycle:
largest_divisor = i
longest_cycle = cycle_count
print(largest_divisor, longest_cycle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment