107 lines
3.5 KiB
Python
107 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Factor 2^m-1 for m=16..1024 via the cyclotomic decomposition
|
|
2^m-1 = prod_{d|m} Phi_d(2).
|
|
Fundamental unit: factor Phi_d(2) for each d=1..1024 (sympy factorint,
|
|
per-d timeout). The distinct prime factors of 2^m-1 are the union of the
|
|
distinct prime factors of Phi_d(2) over d|m; the factorisation is COMPLETE
|
|
iff every Phi_d(2) for d|m was factored completely (product of prime powers
|
|
== Phi_d(2)).
|
|
Output: ~/workspace/rssearch/factors_2m1.json
|
|
{"phi": {d: {"fac": {p: e}, "complete": bool, "bits": int, "time_s": float}},
|
|
"m": {m: {"factors": [distinct primes], "complete": bool}}}
|
|
Re-runnable; resumes from existing file.
|
|
"""
|
|
import json, os, signal, time
|
|
from sympy import factorint
|
|
|
|
OUT = "/home/hatch/workspace/rssearch/factors_2m1.json"
|
|
M_MAX = 1024
|
|
PER_D_TIMEOUT = 120 # seconds per Phi_d(2)
|
|
|
|
def load():
|
|
if os.path.exists(OUT):
|
|
with open(OUT) as f:
|
|
return json.load(f)
|
|
return {"phi": {}, "m": {}}
|
|
|
|
def save(d):
|
|
tmp = OUT + ".tmp"
|
|
with open(tmp, "w") as f:
|
|
json.dump(d, f)
|
|
os.replace(tmp, OUT)
|
|
|
|
def divisors(m):
|
|
return [d for d in range(1, m + 1) if m % d == 0]
|
|
|
|
class Timeout(Exception):
|
|
pass
|
|
|
|
def _handler(signum, frame):
|
|
raise Timeout()
|
|
|
|
def factor_with_timeout(n, timeout):
|
|
signal.signal(signal.SIGALRM, _handler)
|
|
signal.alarm(timeout)
|
|
try:
|
|
return factorint(n, verbose=False)
|
|
finally:
|
|
signal.alarm(0)
|
|
|
|
def main():
|
|
data = load()
|
|
phi = data.setdefault("phi", {})
|
|
mtab = data.setdefault("m", {})
|
|
# Phi_d(2) for d=1..1024 via Mobius inversion over divisors
|
|
phi_val = {1: 1}
|
|
for d in range(2, M_MAX + 1):
|
|
num = (1 << d) - 1
|
|
for dd in divisors(d):
|
|
if dd < d:
|
|
num //= phi_val[dd]
|
|
phi_val[d] = num
|
|
for d in range(1, M_MAX + 1):
|
|
key = str(d)
|
|
if key in phi and phi[key].get("complete"):
|
|
continue
|
|
n = phi_val[d]
|
|
t0 = time.time()
|
|
try:
|
|
fac = factor_with_timeout(n, PER_D_TIMEOUT)
|
|
prod = 1
|
|
for p, e in fac.items():
|
|
prod *= int(p) ** int(e)
|
|
complete = (prod == n)
|
|
fac_s = {str(int(p)): int(e) for p, e in fac.items()}
|
|
except Timeout:
|
|
fac_s, complete = {}, False
|
|
except Exception:
|
|
fac_s, complete = {}, False
|
|
phi[key] = {"fac": fac_s, "complete": bool(complete),
|
|
"bits": n.bit_length(), "time_s": round(time.time() - t0, 2)}
|
|
if d % 100 == 0 or not complete:
|
|
print(f"d={d} bits={n.bit_length()} complete={complete} "
|
|
f"nfac={len(fac_s)} t={time.time()-t0:.1f}s", flush=True)
|
|
save(data)
|
|
# combine into per-m tables
|
|
for m in range(16, M_MAX + 1):
|
|
allf = set()
|
|
ok = True
|
|
for d in divisors(m):
|
|
e = phi.get(str(d))
|
|
if not e or not e.get("complete"):
|
|
ok = False
|
|
if e:
|
|
allf |= {int(p) for p in e["fac"]}
|
|
else:
|
|
allf |= {int(p) for p in e["fac"]}
|
|
mtab[str(m)] = {"factors": sorted(allf), "complete": bool(ok)}
|
|
save(data)
|
|
n_incomplete = sum(1 for v in mtab.values() if not v["complete"])
|
|
print(f"DONE. m=16..1024: {len(mtab)} entries, "
|
|
f"{n_incomplete} with incomplete factorisation.", flush=True)
|
|
print("incomplete m:", [m for m, v in mtab.items() if not v["complete"]][:50],
|
|
flush=True)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|