qs alg update
parent
edbbefa7b4
commit
d5b32dc631
28
QS.py
28
QS.py
|
@ -10,11 +10,13 @@ logging.basicConfig(level=logging.INFO,
|
||||||
filename='quadratic_sieve.log',
|
filename='quadratic_sieve.log',
|
||||||
filemode='w')
|
filemode='w')
|
||||||
|
|
||||||
|
def iammain(): return __name__ == "__main__"
|
||||||
|
|
||||||
def SQ(n):
|
def SQ(n):
|
||||||
def gauss(M):
|
def gauss(M):
|
||||||
marks = [False] * len(M)
|
marks = [False] * len(M)
|
||||||
for j in range(len(M[0])):
|
for j in range(len(M[0])):
|
||||||
print(f"[STEP_2] {j + 1}/{len(M[0])}")
|
if iammain(): print(f"[STEP_2] {j + 1}/{len(M[0])}")
|
||||||
for i in range(len(M)):
|
for i in range(len(M)):
|
||||||
if M[i][j] == 1:
|
if M[i][j] == 1:
|
||||||
marks[i] = True
|
marks[i] = True
|
||||||
|
@ -60,7 +62,7 @@ def SQ(n):
|
||||||
y *= smooth_vals[row][1]
|
y *= smooth_vals[row][1]
|
||||||
s = x
|
s = x
|
||||||
t = isqrt(y)
|
t = isqrt(y)
|
||||||
logging.info(f"Found s and t such that s^2 = t^2 mod n: s = {s}, t = {t}")
|
if iammain(): logging.info(f"Found s and t such that s^2 = t^2 mod n: s = {s}, t = {t}")
|
||||||
return gcd(s - t, n)
|
return gcd(s - t, n)
|
||||||
|
|
||||||
def create_base(n, B):
|
def create_base(n, B):
|
||||||
|
@ -116,8 +118,8 @@ def SQ(n):
|
||||||
start_vals = solve(a, b, N)
|
start_vals = solve(a, b, N)
|
||||||
seen = set()
|
seen = set()
|
||||||
|
|
||||||
logging.info(f"Number of elements in the base: {len(base)}")
|
if iammain(): logging.info(f"Number of elements in the base: {len(base)}")
|
||||||
logging.info(f"Last element in the base: {base[-1]}")
|
if iammain(): logging.info(f"Last element in the base: {base[-1]}")
|
||||||
|
|
||||||
while len(smooth_vals) < needed:
|
while len(smooth_vals) < needed:
|
||||||
sieve_start = sieve_stop
|
sieve_start = sieve_stop
|
||||||
|
@ -142,32 +144,32 @@ def SQ(n):
|
||||||
y = poly(x, a, b, N)
|
y = poly(x, a, b, N)
|
||||||
exp = trial(y, base)
|
exp = trial(y, base)
|
||||||
if tuple(exp) not in seen:
|
if tuple(exp) not in seen:
|
||||||
print(f"[STEP_1] {len(smooth_vals)}/{needed}")
|
if iammain(): print(f"[STEP_1] {len(smooth_vals)}/{needed}")
|
||||||
smooth_vals.append(((a * x) + b, y))
|
smooth_vals.append(((a * x) + b, y))
|
||||||
M.append(exp)
|
M.append(exp)
|
||||||
seen.add(tuple(exp))
|
seen.add(tuple(exp))
|
||||||
|
|
||||||
logging.info(f"Used range of x values: from {sieve_start} to {sieve_stop}")
|
if iammain(): logging.info(f"Used range of x values: from {sieve_start} to {sieve_stop}")
|
||||||
logging.info(f"Result of sieving: found {len(smooth_vals)} smooth numbers")
|
if iammain(): logging.info(f"Result of sieving: found {len(smooth_vals)} smooth numbers")
|
||||||
|
|
||||||
logging.info(f"Example of x and f(x) values: {smooth_vals[:5]}")
|
if iammain(): logging.info(f"Example of x and f(x) values: {smooth_vals[:5]}")
|
||||||
|
|
||||||
logging.info(f"Example of exponent vectors: {[v[:20] + (['...'] if len(v) > 20 else []) for v in M[:5]]}")
|
if iammain(): logging.info(f"Example of exponent vectors: {[v[:20] + (['...'] if len(v) > 20 else []) for v in M[:5]]}")
|
||||||
|
|
||||||
marks, M = gauss(M)
|
marks, M = gauss(M)
|
||||||
|
|
||||||
for i in range(len(marks)):
|
for i in range(len(marks)):
|
||||||
print(f"[STEP_3] {i + 1}/{len(marks)}")
|
if iammain(): print(f"[STEP_3] {i + 1}/{len(marks)}")
|
||||||
if not marks[i]:
|
if not marks[i]:
|
||||||
deps = find_linear_deps(i)
|
deps = find_linear_deps(i)
|
||||||
for dep in deps:
|
for dep in deps:
|
||||||
d = testdep(dep)
|
d = testdep(dep)
|
||||||
if d != 1 and d != N:
|
if d != 1 and d != N:
|
||||||
logging.info(f"Found non-trivial divisor: {d}")
|
if iammain(): logging.info(f"Found non-trivial divisor: {d}")
|
||||||
return d
|
return d
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if iammain():
|
||||||
N1 = 13611197472111783959 # takes 2 seconds
|
N1 = 13611197472111783959 # takes 2 seconds
|
||||||
N2 = 1191515026104746183243378937330489098579 # does not compute
|
N2 = 1191515026104746183243378937330489098579 # does not compute
|
||||||
N3 = 74048093444435937986114388960912781233885985702403356033834092312625704192350369 # does not compute
|
N3 = 74048093444435937986114388960912781233885985702403356033834092312625704192350369 # does not compute
|
||||||
|
@ -175,6 +177,6 @@ if __name__ == "__main__":
|
||||||
number = N1
|
number = N1
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
print('d=', SQ(number))
|
SQ(number)
|
||||||
elapsed_time = time.time() - start_time
|
elapsed_time = time.time() - start_time
|
||||||
logging.info(f"Total program execution time: {elapsed_time} seconds.")
|
logging.info(f"Total program execution time: {elapsed_time} seconds.")
|
Loading…
Reference in New Issue