Skip to content

Instantly share code, notes, and snippets.

@tomfa
Created April 25, 2020 19:43
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 tomfa/10db6f7f999c98cf7c1899675786286d to your computer and use it in GitHub Desktop.
Save tomfa/10db6f7f999c98cf7c1899675786286d to your computer and use it in GitHub Desktop.
from decimal import *
getcontext().prec = 2000
def find_recurring_length(divisor):
digits = (Decimal(1) / Decimal(divisor)).as_tuple().digits[:-1]
precision = getcontext().prec -1
if len(digits) != precision:
return 0
recurring = 0
for i in range(0, 7):
l_digits = digits[i:]
l = len(l_digits)
for j in range(1, int(l / 2)):
multiplier = int(l/j)
if l_digits[:j] * (multiplier) == l_digits[:multiplier * j]:
return j
assert False, f'Could not find recurrence for {divisor}'
def findit():
longest_reccuring = (0, 0)
for i in range(2, 1000):
_, length = longest_reccuring
ans = find_recurring_length(i)
if ans > length:
longest_reccuring = (i, ans)
print(f'{ans} > {length} for {i}')
def works():
assert find_recurring_length(3) == 1, f'{3} != 1'
print(f'{(3)} works')
assert find_recurring_length(6) == 1, f'{6} != 1'
print(f'{(6)} works')
assert find_recurring_length(7) == 6, f'{7} != 6'
print(f'{(7)} works')
assert find_recurring_length(9) == 1, f'{9} != 1'
print(f'{(9)} works')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment