Created
July 24, 2022 17:31
-
-
Save homedirectory/499e85d541aa9ecb1133ebd9c92b51c2 to your computer and use it in GitHub Desktop.
HPMOR - Time-Turner prime numbers experiment
This file contains hidden or 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
# Chapter 17 of HPMOR | |
# The experiment with prime numbers | |
# python doesn't support tail recursion, so do this with a while loop | |
def run(x, y): | |
while True: | |
# paper 2 is blank | |
if x is None and y is None: | |
x, y = 101, 101 | |
elif x == 997 and y == 997: | |
x, y = None, None | |
else: | |
prod = x * y | |
if prod == 181429: | |
# Magic! Now you can compute prime factors of any prime number | |
# I guess that 101 and 997 were chosen by the author[s] to set the lower and upper bound | |
# Harry would have to continue the loop by sending the numbers back in time, | |
# but we can stop our program | |
print("{} x {} = 181429".format(x,y)) | |
return x, y | |
else: | |
x1 = x | |
y1 = y + 2 | |
if y1 > 997: | |
x1 = x + 2 | |
y1 = 101 | |
x, y = x1, y1 | |
if __name__ == "__main__": | |
# set initial numbers (try different variants) | |
# 1. 101 x 101 | |
x, y = 101, 101 | |
# 2. blank paper | |
#x, y = None, None | |
run(x, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment