Created
April 26, 2022 20:48
-
-
Save Leowbattle/67d36858dc987e6b5a687f21d87d1a47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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