Skip to content

Commit bb2c3bc

Browse files
author
Sourcery AI
committed
'Refactored by Sourcery'
1 parent c948266 commit bb2c3bc

File tree

4 files changed

+105
-122
lines changed

4 files changed

+105
-122
lines changed

MPQS.sage

+37-38
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ def prebuilt_params(bits):
6969
return [900000, 150, 50 * 65536]
7070
if bits <= 490:
7171
return [1100000, 150, 75 * 65536]
72-
if bits <= 512:
73-
return [1300000, 150, 100 * 65536]
7472
return [1300000, 150, 100 * 65536]
7573

7674

@@ -113,10 +111,10 @@ def choose_multiplier(n, prime_list):
113111
if knmod8 == 5:
114112
scores[i] -= M_LN2;
115113
break;
116-
if knmod8 == 3 or knmod8 == 7:
114+
if knmod8 in [3, 7]:
117115
scores[i] -= 0.5 * M_LN2;
118116
break;
119-
# even multipliers start with a handicap
117+
# even multipliers start with a handicap
120118
num_multipliers = i;
121119

122120
# for the rest of the small factor base primes
@@ -131,7 +129,7 @@ def choose_multiplier(n, prime_list):
131129
knmodp = (curr_mult * modp) % prime
132130
# if prime i is actually in the factor base
133131
# for k * n ... */
134-
if (knmodp == 0 or legendre(knmodp, prime) == 1):
132+
if knmodp == 0:
135133

136134
""" ...add its contribution. A prime p con-
137135
tributes log(p) to 1 in p sieve values, plus
@@ -147,18 +145,31 @@ def choose_multiplier(n, prime_list):
147145
the prime divides k*n. In that case there
148146
is only one root """
149147

150-
if (knmodp == 0):
151-
scores[j] -= contrib
152-
else:
153-
scores[j] -= 2 * contrib
148+
scores[j] -= contrib
149+
elif legendre(knmodp, prime) == 1:
150+
151+
""" ...add its contribution. A prime p con-
152+
tributes log(p) to 1 in p sieve values, plus
153+
log(p) to 1 in p^2 sieve values, etc. The
154+
average contribution of all multiples of p
155+
to a random sieve value is thus
156+
log(p) * (1/p + 1/p^2 + 1/p^3 + ...)
157+
= (log(p) / p) * 1 / (1 - (1/p))
158+
= log(p) / (p-1)
159+
This contribution occurs once for each
160+
square root used for sieving. There are two
161+
roots for each factor base prime, unless
162+
the prime divides k*n. In that case there
163+
is only one root """
154164

165+
scores[j] -= contrib if (knmodp == 0) else 2 * contrib
155166
# use the multiplier that generates the best score
156167

157168
best_score = 1000.0
158169
best_mult = 1
159170

160171
#print(scores)
161-
172+
162173
for i in range(0, num_multipliers):
163174
score = scores[i];
164175
if (score < best_score):
@@ -212,15 +223,14 @@ def trial_division(n, P):
212223
l = len(P)
213224
i = 0
214225
for p in P:
215-
if r >= p:
216-
if r % p == 0:
217-
pw = 0
218-
while r % p == 0:
219-
pw += 1
220-
r //= p
221-
a.append((int(p),int(pw)))
222-
else:
226+
if r < p:
223227
break
228+
if r % p == 0:
229+
pw = 0
230+
while r % p == 0:
231+
pw += 1
232+
r //= p
233+
a.append((int(p),int(pw)))
224234
return a,r,n
225235

226236

@@ -234,10 +244,7 @@ def merge_powers(ppws):
234244
d[p] = pw
235245
else:
236246
d[p] += pw
237-
tmp2 = []
238-
for p in d:
239-
tmp2.append((p,d[p]))
240-
return tmp2
247+
return list(d.items())
241248

242249

243250
def filter_out_even_powers(ppws):
@@ -250,11 +257,7 @@ def filter_out_even_powers(ppws):
250257
d[p] = pw
251258
else:
252259
d[p] += pw
253-
tmp2 = []
254-
for p in d:
255-
if d[p] & 1 != 0: # only keep odd powers
256-
tmp2.append(p)
257-
return tmp2
260+
return [p for p, value in d.items() if value & 1 != 0]
258261

259262

260263
def trial_division_minus_even_powers(n, P):
@@ -297,13 +300,12 @@ def is_power_logprime(n, log_primes):
297300
Given the precomputed logs of a set of primes
298301
we can test if a number n is a perfect power of that prime.
299302
"""
300-
ispow = False
301303
for p in log_primes:
302304
a = log(n) / p
303305
b = int(a)
304306
if a == b:
305307
iwpow = True
306-
return ispow
308+
return False
307309

308310

309311
def minifactor4(x, P, smooth_base):
@@ -337,7 +339,7 @@ class Poly:
337339
#self.counter = 0
338340
self.early_factors = []
339341

340-
if A == None and B == None and C == None:
342+
if A is None and B is None and C is None:
341343
self.create()
342344
else:
343345
print("given poly parameters: %d %d %d" % (A,B,C))
@@ -401,15 +403,15 @@ class Poly:
401403
Code borrowed https://github.com/cramppet/quadratic-sieve/blob/master/QS.py
402404
"""
403405
A = self.A
404-
B = self.B
405406
C = self.C
406407
n = self.n
407408
p = self.root_A
408409
if A > 1:
409410
g = 1
411+
ainv = 1
412+
B = self.B
410413
while g == 1:
411414
p = next_prime(p)
412-
ainv = 1
413415
if A != 1:
414416
g, inv, _ = gcdext(A, p)
415417
if g != 1:
@@ -442,9 +444,8 @@ class Poly:
442444
"""
443445
Return the string representation of the constructed polynomial
444446
"""
445-
m = "F(X) = %d X ^ 2 + %d X + %d with minima: %d" % (self.A, self.B, self.C, self.minima)
446-
m = m.replace("+ -","- ")
447-
return m
447+
m = "F(X) = %d X ^ 2 + %d X + %d with minima: %d" % (self.A, self.B, self.C, self.minima)
448+
return m.replace("+ -","- ")
448449

449450

450451
def __add__(self, other):
@@ -486,9 +487,7 @@ class Poly:
486487
"""
487488
Hashes unique values of the polynomial to construct an python internal id.
488489
"""
489-
h = hash("%d-%d-%d" % (self.A,self.B,self.C))
490-
#print("hash: %s" % h)
491-
return h
490+
return hash("%d-%d-%d" % (self.A,self.B,self.C))
492491

493492

494493
def __eq__(self, other):

PisanoPeriod.py

+55-69
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ def _fib_res(self,n,p):
2323
""" fibonacci sequence nth item modulo p """
2424
if n == 0:
2525
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))
3430

3531

3632
def get_n_mod_d(self,n,d, use = 'mersenne'):
@@ -45,9 +41,7 @@ def get_n_mod_d(self,n,d, use = 'mersenne'):
4541

4642
def ranged_period(self, N, start, stop, look_up_dest):
4743
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)}
5145
look_up_dest.update(tmp_look_up)
5246
#look_up_dest |= tmp_look_up
5347
print("ranged_period (%d,%d) end" % (start,stop))
@@ -56,24 +50,22 @@ def ranged_period(self, N, start, stop, look_up_dest):
5650

5751
def get_period_bigint(self, N, min_accept, xdiff, verbose = False):
5852
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)
6355
if self.verbose:
6456
print('Search_len: %d, log2(N): %d' % (search_len,int(log2(N))))
65-
57+
6658
starttime = time.time()
67-
diff = xdiff
59+
diff = xdiff
6860
p_len = int((len(str(N)) + diff) >> 1) + 1
69-
begin = N - int('9'*p_len)
61+
begin = N - int('9'*p_len)
7062
if begin <= 0:
7163
begin = 1
7264
end = N + int('9' * p_len)
73-
65+
7466
if self.verbose:
7567
print('Search begin: %d, end: %d'%(begin, end))
76-
68+
7769
if self.multitasked and search_len > 1000:
7870
C = cpu_count() * 2
7971
search_work = search_len // C
@@ -84,10 +76,10 @@ def get_period_bigint(self, N, min_accept, xdiff, verbose = False):
8476
if self.verbose:
8577
print("Precompute LUT with %d tasks..." % C)
8678

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+
]
9183
workpool = Pool(C)
9284

9385
with workpool:
@@ -103,10 +95,10 @@ def get_period_bigint(self, N, min_accept, xdiff, verbose = False):
10395
if self.verbose:
10496
print("LUT creation ended size: %d..." % len(look_up))
10597
print("Searching...")
106-
10798

108-
while True:
109-
randi = random.randint(begin,end)
99+
100+
while True:
101+
randi = random.randint(begin,end)
110102
res = self.get_n_mod_d(randi, N)
111103
if res > 0:
112104
#print(res, res in look_up)
@@ -122,13 +114,15 @@ def get_period_bigint(self, N, min_accept, xdiff, verbose = False):
122114
else:
123115
if self.verbose:
124116
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
132126

133127

134128
def _trivial_factorization_with_n_phi(self, N, T):
@@ -142,24 +136,16 @@ def _trivial_factorization_with_n_phi(self, N, T):
142136

143137
if phi2m4d > 0:
144138
iphi2m4d = isqrt(phi2m4d)
145-
p1.append(phi + iphi2m4d)
146-
p1.append(phi - iphi2m4d)
147-
139+
p1.extend((phi + iphi2m4d, phi - iphi2m4d))
148140
if phi2p4d > 0:
149141
iphi2p4d = isqrt(phi2p4d)
150-
p1.append(phi + iphi2p4d)
151-
p1.append(phi - iphi2p4d)
152-
142+
p1.extend((phi + iphi2p4d, phi - iphi2p4d))
153143
if phi2m4d > 0:
154144
iphi2m4d = isqrt(phi2m4d)
155-
p1.append(-phi + iphi2m4d)
156-
p1.append(-phi - iphi2m4d)
157-
145+
p1.extend((-phi + iphi2m4d, -phi - iphi2m4d))
158146
if phi2p4d > 0:
159147
iphi2p4d = isqrt(phi2p4d)
160-
p1.append(-phi + iphi2p4d)
161-
p1.append(-phi - iphi2p4d)
162-
148+
p1.extend((-phi + iphi2p4d, -phi - iphi2p4d))
163149
for p in p1:
164150
g = gcd((p >> 1),N)
165151
if N > g > 1:
@@ -224,7 +210,7 @@ def test(Ns,B2=0):
224210
if P != None:
225211
phi = (P[0]-1) * (P[1]-1)
226212
print("phi(N): %d" % phi)
227-
print("factors: %s" % str(P))
213+
print(f"factors: {str(P)}")
228214
ff += 1
229215
td = time.time() - ti
230216
ttd = time.time() - tti
@@ -235,27 +221,27 @@ def test(Ns,B2=0):
235221

236222

237223
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\nFully 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\nFully factored:%d of %d" % (td,ff,l))
243+
print("Runtime total: %f" % (ttd))
244+
n += 1
259245

260246

261247
def test3(N, B2 = 0):
@@ -291,7 +277,7 @@ def test4(l,B2=0):
291277
if P != None:
292278
phi = (P[0]-1) * (P[1]-1)
293279
print("phi(N): %d" % phi)
294-
print("factors: %s" % str(P))
280+
print(f"factors: {str(P)}")
295281
ff += 1
296282
td = time.time() - ti
297283
ttd = time.time() - tti

0 commit comments

Comments
 (0)