Last active
April 17, 2019 14:37
-
-
Save naterush/fc67f256f6c94993fbae1237faea66d9 to your computer and use it in GitHub Desktop.
Help me figure out how much this thing fails!
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
import math | |
import time | |
def f(x): | |
return x**2 + 1 | |
def pollard_rho(N): | |
xn = 2 | |
x2n = 2 | |
#print("xn: {}, x2n: {}".format(xn, x2n)) | |
d = 1 | |
while d == 1: | |
xn = f(xn) % N | |
x2n = f(f(x2n)) % N | |
#print("xn: {}, x2n: {}".format(xn, x2n)) | |
abs_val = abs(xn - x2n) | |
d = math.gcd(abs_val, N) | |
if d == N: | |
return False | |
else: | |
return d, N//d | |
def calc_perc_failure(): | |
val = max(2, int(input("What number would you like to check up to? "))) | |
success = 0 | |
failure = 0 | |
for factor in range(2, val): | |
res = pollard_rho(factor) | |
if not res: | |
failure += 1 | |
else: | |
success += 1 | |
print("Failed on {} of integers up to {}".format(failure / (success + failure), val)) | |
while True: | |
calc_perc_failure() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment