qs alg update

master
serr 2025-02-22 20:30:58 +03:00
parent edbbefa7b4
commit d5b32dc631
1 changed files with 15 additions and 13 deletions

28
QS.py
View File

@ -10,11 +10,13 @@ logging.basicConfig(level=logging.INFO,
filename='quadratic_sieve.log',
filemode='w')
def iammain(): return __name__ == "__main__"
def SQ(n):
def gauss(M):
marks = [False] * len(M)
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)):
if M[i][j] == 1:
marks[i] = True
@ -60,7 +62,7 @@ def SQ(n):
y *= smooth_vals[row][1]
s = x
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)
def create_base(n, B):
@ -116,8 +118,8 @@ def SQ(n):
start_vals = solve(a, b, N)
seen = set()
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"Number of elements in the base: {len(base)}")
if iammain(): logging.info(f"Last element in the base: {base[-1]}")
while len(smooth_vals) < needed:
sieve_start = sieve_stop
@ -142,32 +144,32 @@ def SQ(n):
y = poly(x, a, b, N)
exp = trial(y, base)
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))
M.append(exp)
seen.add(tuple(exp))
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"Used range of x values: from {sieve_start} to {sieve_stop}")
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)
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]:
deps = find_linear_deps(i)
for dep in deps:
d = testdep(dep)
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 None
if __name__ == "__main__":
if iammain():
N1 = 13611197472111783959 # takes 2 seconds
N2 = 1191515026104746183243378937330489098579 # does not compute
N3 = 74048093444435937986114388960912781233885985702403356033834092312625704192350369 # does not compute
@ -175,6 +177,6 @@ if __name__ == "__main__":
number = N1
start_time = time.time()
print('d=', SQ(number))
SQ(number)
elapsed_time = time.time() - start_time
logging.info(f"Total program execution time: {elapsed_time} seconds.")