Skip to content

Instantly share code, notes, and snippets.

@Leowbattle
Created April 26, 2022 20:48
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 Leowbattle/67d36858dc987e6b5a687f21d87d1a47 to your computer and use it in GitHub Desktop.
Save Leowbattle/67d36858dc987e6b5a687f21d87d1a47 to your computer and use it in GitHub Desktop.
# Does the decimal expansion of 1/q terminate?
def terminates(q):
while q % 2 == 0:
q //= 2
while q % 5 == 0:
q //= 5
return q == 1
# If the decimal expansion of 1/q terminates return 0, else return the period of the repeating digits.
def period(q):
if terminates(q):
return 0
rems = []
p = 1
i = 0
while True:
rem = p % q
if rem in rems:
return i - rems.index(rem)
i += 1
rems.append(rem)
p = 10 * rem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment