-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdixon2.py
53 lines (41 loc) · 1.02 KB
/
dixon2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from gmpy2 import isqrt,gcd,next_prime, is_prime
import sys
import bitarray
def dixon(N,B=7,explain=False):
if is_prime(N):
return N,1
start = isqrt(N)
if (start ** 2) == N:
return start,start
def primes(B):
p = 1
tmp = []
while p < B:
tmp.append(p)
p = next_prime(p)
return tmp
base = primes(B)
lqbf = pow(base[-1],2)+1
QBF = bitarray.bitarray(lqbf)
i = start
basej2N = []
for j in range(0,len(base)):
p = pow(base[j],2,N)
basej2N.append(p)
QBFp] = 1
while i < N:
i2N = pow(i,2,N)
if i2N < lqbf and QBF[i2N] == 1:
for k in range(0,len(base)):
if QBF[basej2N[k]] == 1:
#if i2N == basej2N[k]:
f=gcd(i - base[k],N)
if explain:
print("%d = pow(%d,2,n)" % (i2N,i))
print("%d = pow(%d,2,n)" % (basej2N[k],base[k]))
print("%d - %d = %d" % (i,base[k],f))
if 1 < f < N:
return f,N//f
i+=1
return -1
print(dixon(int(sys.argv[1]),B=int(sys.argv[2])))