Created
April 22, 2014 17:33
-
-
Save silveira/11187839 to your computer and use it in GitHub Desktop.
Pollard’s rho algorithm for factoring integers
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
def gcd(a, b): | |
while b: | |
a, b = b, a%b | |
return a | |
def f(x,n): | |
return (x*x-1)%n | |
def rho(n): | |
x = 2 | |
y = 2 | |
d = 1 | |
while d==1: | |
print x,y,d | |
x = f(x,n) | |
y = f(f(y,n),n) | |
d = gcd(abs(x-y),n) | |
if d==n: | |
return False | |
return d | |
print rho(455459) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or def f(x,n): return (x*x+1)%n