@@ -23,14 +23,10 @@ def _fib_res(self,n,p):
23
23
""" fibonacci sequence nth item modulo p """
24
24
if n == 0 :
25
25
return (0 , 1 )
26
- else :
27
- a , b = self ._fib_res (n >> 1 ,p )
28
- c = mod ((mod (a , p ) * mod (((b << 1 ) - a ), p )), p )
29
- d = mod ((powmod (a , 2 , p ) + powmod (b , 2 , p )), p )
30
- if n & 1 == 0 :
31
- return (c , d )
32
- else :
33
- return (d , mod ((c + d ), p ))
26
+ a , b = self ._fib_res (n >> 1 ,p )
27
+ c = mod ((mod (a , p ) * mod (((b << 1 ) - a ), p )), p )
28
+ d = mod ((powmod (a , 2 , p ) + powmod (b , 2 , p )), p )
29
+ return (c , d ) if n & 1 == 0 else (d , mod ((c + d ), p ))
34
30
35
31
36
32
def get_n_mod_d (self ,n ,d , use = 'mersenne' ):
@@ -45,9 +41,7 @@ def get_n_mod_d(self,n,d, use = 'mersenne'):
45
41
46
42
def ranged_period (self , N , start , stop , look_up_dest ):
47
43
print ("ranged_period (%d,%d) start" % (start ,stop ))
48
- tmp_look_up = {}
49
- for x in range (start , stop ):
50
- tmp_look_up [self .get_n_mod_d (x , N )] = x
44
+ tmp_look_up = {self .get_n_mod_d (x , N ): x for x in range (start , stop )}
51
45
look_up_dest .update (tmp_look_up )
52
46
#look_up_dest |= tmp_look_up
53
47
print ("ranged_period (%d,%d) end" % (start ,stop ))
@@ -56,24 +50,22 @@ def ranged_period(self, N, start, stop, look_up_dest):
56
50
57
51
def get_period_bigint (self , N , min_accept , xdiff , verbose = False ):
58
52
search_len = int (pow (N , (1.0 / 6 ) / 100 ))
59
-
60
- if search_len < min_accept :
61
- search_len = min_accept
62
-
53
+
54
+ search_len = max (search_len , min_accept )
63
55
if self .verbose :
64
56
print ('Search_len: %d, log2(N): %d' % (search_len ,int (log2 (N ))))
65
-
57
+
66
58
starttime = time .time ()
67
- diff = xdiff
59
+ diff = xdiff
68
60
p_len = int ((len (str (N )) + diff ) >> 1 ) + 1
69
- begin = N - int ('9' * p_len )
61
+ begin = N - int ('9' * p_len )
70
62
if begin <= 0 :
71
63
begin = 1
72
64
end = N + int ('9' * p_len )
73
-
65
+
74
66
if self .verbose :
75
67
print ('Search begin: %d, end: %d' % (begin , end ))
76
-
68
+
77
69
if self .multitasked and search_len > 1000 :
78
70
C = cpu_count () * 2
79
71
search_work = search_len // C
@@ -84,10 +76,10 @@ def get_period_bigint(self, N, min_accept, xdiff, verbose = False):
84
76
if self .verbose :
85
77
print ("Precompute LUT with %d tasks..." % C )
86
78
87
- inputs = []
88
- for x in range ( 0 , search_len , search_work ):
89
- inputs += [( N , x , x + search_work , look_up )]
90
-
79
+ inputs = [
80
+ ( N , x , x + search_work , look_up )
81
+ for x in range ( 0 , search_len , search_work )
82
+ ]
91
83
workpool = Pool (C )
92
84
93
85
with workpool :
@@ -103,10 +95,10 @@ def get_period_bigint(self, N, min_accept, xdiff, verbose = False):
103
95
if self .verbose :
104
96
print ("LUT creation ended size: %d..." % len (look_up ))
105
97
print ("Searching..." )
106
-
107
98
108
- while True :
109
- randi = random .randint (begin ,end )
99
+
100
+ while True :
101
+ randi = random .randint (begin ,end )
110
102
res = self .get_n_mod_d (randi , N )
111
103
if res > 0 :
112
104
#print(res, res in look_up)
@@ -122,13 +114,15 @@ def get_period_bigint(self, N, min_accept, xdiff, verbose = False):
122
114
else :
123
115
if self .verbose :
124
116
print ('For N = %d\n Found res: %d, res_n: %d , T: %d\n but failed!' % (N , res , res_n , T ))
125
- else :
126
- if randi & 1 == 0 :
127
- T = randi
128
- td = int (time .time () - starttime )
129
- if self .verbose :
130
- print ('First shot, For N = %d Found T:%d, randi: %d, time used %f secs.' % (N , T , randi , td ))
131
- return td , T , randi
117
+ elif randi & 1 == 0 :
118
+ td = int (time .time () - starttime )
119
+ T = randi
120
+ if self .verbose :
121
+ print (
122
+ 'First shot, For N = %d Found T:%d, randi: %d, time used %f secs.'
123
+ % (N , T , T , td )
124
+ )
125
+ return td , T , T
132
126
133
127
134
128
def _trivial_factorization_with_n_phi (self , N , T ):
@@ -142,24 +136,16 @@ def _trivial_factorization_with_n_phi(self, N, T):
142
136
143
137
if phi2m4d > 0 :
144
138
iphi2m4d = isqrt (phi2m4d )
145
- p1 .append (phi + iphi2m4d )
146
- p1 .append (phi - iphi2m4d )
147
-
139
+ p1 .extend ((phi + iphi2m4d , phi - iphi2m4d ))
148
140
if phi2p4d > 0 :
149
141
iphi2p4d = isqrt (phi2p4d )
150
- p1 .append (phi + iphi2p4d )
151
- p1 .append (phi - iphi2p4d )
152
-
142
+ p1 .extend ((phi + iphi2p4d , phi - iphi2p4d ))
153
143
if phi2m4d > 0 :
154
144
iphi2m4d = isqrt (phi2m4d )
155
- p1 .append (- phi + iphi2m4d )
156
- p1 .append (- phi - iphi2m4d )
157
-
145
+ p1 .extend ((- phi + iphi2m4d , - phi - iphi2m4d ))
158
146
if phi2p4d > 0 :
159
147
iphi2p4d = isqrt (phi2p4d )
160
- p1 .append (- phi + iphi2p4d )
161
- p1 .append (- phi - iphi2p4d )
162
-
148
+ p1 .extend ((- phi + iphi2p4d , - phi - iphi2p4d ))
163
149
for p in p1 :
164
150
g = gcd ((p >> 1 ),N )
165
151
if N > g > 1 :
@@ -224,7 +210,7 @@ def test(Ns,B2=0):
224
210
if P != None :
225
211
phi = (P [0 ]- 1 ) * (P [1 ]- 1 )
226
212
print ("phi(N): %d" % phi )
227
- print ("factors: %s" % str (P ))
213
+ print (f "factors: { str (P )} " )
228
214
ff += 1
229
215
td = time .time () - ti
230
216
ttd = time .time () - tti
@@ -235,27 +221,27 @@ def test(Ns,B2=0):
235
221
236
222
237
223
def test2 ():
238
- Fib = Fibonacci ()
239
- N = 384237701921
240
- B1s = [10 ** x for x in range (6 ,3 ,- 1 )]
241
- B2s = [0 ,2 ,4 ,6 ,8 ]
242
- n = 1
243
- tti = time .time ()
244
- for B1 in B1s :
245
- for B2 in B2s :
246
- ti = time .time ()
247
- print ("Test: %d, N: %d, log2(N): %d, B1: %d, B2: %d" % (n , N ,int (log2 (N )),B1 ,B2 ))
248
- P = Fib .factorization (N ,B1 ,B2 )
249
- if P != None :
250
- phi = (P [0 ]- 1 ) * (P [1 ]- 1 )
251
- print ("phi(N): %d" % phi )
252
- print ("factors: %s" % str (P ))
253
- ff += 1
254
- td = time .time () - ti
255
- ttd = time .time () - tti
256
- print ("Runtime: %f\n Fully factored:%d of %d" % (td ,ff ,l ))
257
- print ("Runtime total: %f" % (ttd ))
258
- n += 1
224
+ Fib = Fibonacci ()
225
+ N = 384237701921
226
+ B1s = [10 ** x for x in range (6 ,3 ,- 1 )]
227
+ B2s = [0 ,2 ,4 ,6 ,8 ]
228
+ n = 1
229
+ tti = time .time ()
230
+ for B1 in B1s :
231
+ for B2 in B2s :
232
+ ti = time .time ()
233
+ print ("Test: %d, N: %d, log2(N): %d, B1: %d, B2: %d" % (n , N ,int (log2 (N )),B1 ,B2 ))
234
+ P = Fib .factorization (N ,B1 ,B2 )
235
+ if P != None :
236
+ phi = (P [0 ]- 1 ) * (P [1 ]- 1 )
237
+ print ("phi(N): %d" % phi )
238
+ print (f "factors: { str (P )} " )
239
+ ff += 1
240
+ td = time .time () - ti
241
+ ttd = time .time () - tti
242
+ print ("Runtime: %f\n Fully factored:%d of %d" % (td ,ff ,l ))
243
+ print ("Runtime total: %f" % (ttd ))
244
+ n += 1
259
245
260
246
261
247
def test3 (N , B2 = 0 ):
@@ -291,7 +277,7 @@ def test4(l,B2=0):
291
277
if P != None :
292
278
phi = (P [0 ]- 1 ) * (P [1 ]- 1 )
293
279
print ("phi(N): %d" % phi )
294
- print ("factors: %s" % str (P ))
280
+ print (f "factors: { str (P )} " )
295
281
ff += 1
296
282
td = time .time () - ti
297
283
ttd = time .time () - tti
0 commit comments