Skip to content

Commit 9af4e06

Browse files
authored
if is_prime(n): return n
1 parent 3584fa7 commit 9af4e06

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

pollard_rho.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from gmpy2 import isqrt, gcd, is_prime
2+
import sys
3+
4+
def pollard_rho(self, n, seed=2, p=2, mode=1):
5+
if is_prime(n):
6+
return n
7+
if n % 2 == 0:
8+
return 2
9+
if n % 3 == 0:
10+
return 3
11+
if n % 5 == 0:
12+
return 5
13+
if mode == 1:
14+
f = lambda x: x ** p + 1
15+
else:
16+
f = lambda x: x ** p - 1
17+
x, y, d = seed, seed, 1
18+
while d == 1:
19+
x = f(x) % n
20+
y = f(f(y)) % n
21+
d = gcd((x - y) % n, n)
22+
return None if d == n else d
23+
24+
print(int(sys.argv[1]))

0 commit comments

Comments
 (0)