forked from google-deepmind/formal-conjectures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_extensions.py
More file actions
530 lines (459 loc) · 16.6 KB
/
Copy pathoperator_extensions.py
File metadata and controls
530 lines (459 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
"""
operator_extensions.py — Operator algebra for remaining open problems.
Extends ErdosSolverDB with operators for:
- Twin primes (Hardy-Littlewood, Brun's sieve, GP-hat at gap-2)
- Goldbach binary (Selberg sieve, M_K antisymmetric pairs)
- Sidon sets (additive combinatorics)
- Ramsey R(3,k) (probabilistic lower bounds)
- Brocard, Beal, Catalan (Diophantine)
- Erdős-Moser, Lehmer's totient
- Andrica's gap conjecture
- Cramér's gap conjecture
- Polignac (gap k infinitely often)
Each operator: defined, applied, results stored in DB.
"""
import math
import time
import sqlite3
from math import gcd, isqrt
from sympy import isprime, primerange, factorint, divisors, totient, mobius
from math import comb
PHI = (1 + math.sqrt(5)) / 2
SIGMA = math.log(PHI)
K = 107
S_ATLAS = 214
N_C = 3
N_ST = 4
ICAS = 29
M_840 = 840
# Hardy-Littlewood twin prime constant (precomputed)
C_2 = 0.6601682965
def L(n):
if n == 0: return 2
if n == 1: return 1
a, b = 2, 1
for _ in range(n - 1):
a, b = b, a + b
return b
# ============================================================
# OPERATORS — TWIN PRIMES
# ============================================================
class TwinPrimeOps:
"""Operators for twin prime conjecture."""
@staticmethod
def T_hat(N):
"""Exact count of twin primes ≤ N."""
return sum(1 for p in primerange(2, N) if isprime(p + 2))
@staticmethod
def T_hat_pairs(N, max_pairs=20):
"""First few twin prime pairs ≤ N."""
out = []
for p in primerange(2, N):
if isprime(p + 2):
out.append((p, p + 2))
if len(out) >= max_pairs: break
return out
@staticmethod
def HL_hat(N):
"""Hardy-Littlewood asymptotic: 2 C_2 N / (log N)²."""
if N < 3: return 0.0
return 2 * C_2 * N / (math.log(N) ** 2)
@staticmethod
def Brun_hat(N):
"""Brun's sieve upper bound: 8 C_2 N / (log N)²."""
if N < 3: return 0.0
return 8 * C_2 * N / (math.log(N) ** 2)
@staticmethod
def GP_gap2(c):
"""GP-hat at gap-2: number of (p, p+2) twin pairs with p ≤ c."""
return sum(1 for p in primerange(2, c) if isprime(p + 2))
@staticmethod
def Pi2_at_anchor():
"""π_2(214) at framework anchor S."""
return TwinPrimeOps.T_hat(S_ATLAS)
# ============================================================
# OPERATORS — GOLDBACH
# ============================================================
class GoldbachOps:
"""Operators for Goldbach binary conjecture."""
@staticmethod
def G_hat(c):
"""G(2c) = number of Goldbach pairs (p, q) with p ≤ q, p + q = 2c, both prime."""
n = 2 * c
if n < 4: return 0
return sum(1 for p in primerange(2, c + 1) if isprime(n - p))
@staticmethod
def Selberg_lambda(n, z):
"""Approximate Selberg sieve weight."""
return sum(mobius(d) ** 2 for d in divisors(n) if d <= z)
@staticmethod
def G_lower_HL(c):
"""Hardy-Littlewood asymptotic for G(2c). Singular series + main term."""
if c < 2: return 0.0
# G(2c) ~ 2 C_2 · c/(log c)^2 · ∏_{p|c, p>2} (p-1)/(p-2)
if c < 3: return 0.0
prod_term = 1.0
for p in factorint(c).keys():
if p > 2:
prod_term *= (p - 1) / (p - 2)
return 2 * C_2 * c / (math.log(c) ** 2) * prod_term
@staticmethod
def M_K_antisymmetric(c):
"""Pairs (p, q) with M_K(p) + M_K(q) = 0, i.e. p + q = 2K = S."""
if c != K: return None
return GoldbachOps.G_hat(c)
# ============================================================
# OPERATORS — SIDON SETS
# ============================================================
class SidonOps:
"""Sidon set operators: pairwise distinct sums."""
@staticmethod
def is_Sidon(S):
sums = set()
S = list(S)
for i in range(len(S)):
for j in range(i, len(S)):
v = S[i] + S[j]
if v in sums: return False
sums.add(v)
return True
@staticmethod
def max_Sidon_in(N):
"""Greedy maximal Sidon subset of [1, N]."""
S = [1]
sums = {2}
for n in range(2, N + 1):
# Check if n can be added
valid = True
new_sums = set()
for s in S:
v = s + n
if v in sums or v in new_sums:
valid = False; break
new_sums.add(v)
new_sums.add(2 * n)
if 2 * n in sums:
valid = False
if valid:
S.append(n)
sums |= new_sums
return S
@staticmethod
def Sidon_density_bound(N):
"""Erdős-Turán: |S| ≤ √N + O(N^{1/4}) for Sidon S ⊆ [1,N]."""
if N < 1: return 0
return math.sqrt(N) + N ** 0.25
# ============================================================
# OPERATORS — RAMSEY
# ============================================================
class RamseyOps:
"""Ramsey number operators."""
KNOWN_R3k = {3: 6, 4: 9, 5: 14, 6: 18, 7: 23, 8: 28, 9: 36}
@staticmethod
def R_hat(s, k):
"""Known Ramsey numbers."""
if s == 3 and k in RamseyOps.KNOWN_R3k:
return RamseyOps.KNOWN_R3k[k]
return None
@staticmethod
def lower_bound_CJMS(k):
"""Campos-Jenssen-Michelen-Sahasrabudhe 2025: R(3,k) ≥ k²/(log k)² · (1+o(1))."""
if k < 2: return None
return k * k / (math.log(k) ** 2)
@staticmethod
def upper_bound_Shearer(k):
"""Shearer 1983: R(3,k) ≤ k² / log k · (1 + o(1))."""
if k < 2: return None
return k * k / math.log(k)
# ============================================================
# OPERATORS — DIOPHANTINE
# ============================================================
class DiophantineOps:
"""Operators for Brocard, Beal, Catalan, Erdős-Moser, Lehmer."""
@staticmethod
def Brocard_check(n):
f = math.factorial(n) + 1
m = isqrt(f)
return (m, m * m == f)
@staticmethod
def Beal_search(max_base=20, max_exp=5):
ces = []
for x in range(1, max_base):
for y in range(1, max_base):
for a in range(3, max_exp + 1):
for b in range(3, max_exp + 1):
val = x ** a + y ** b
for c in range(3, max_exp + 1):
z = round(val ** (1 / c))
if z > 0 and z ** c == val:
g = gcd(gcd(x, y), z)
if g == 1:
ces.append((x, a, y, b, z, c))
return ces
@staticmethod
def Catalan_search(max_base=50, max_exp=7):
sols = []
for x in range(2, max_base):
for p in range(2, max_exp + 1):
for y in range(2, max_base):
for q in range(2, max_exp + 1):
if x ** p - y ** q == 1:
sols.append((x, p, y, q))
return sols
@staticmethod
def Erdos_Moser_check(k_max=4, m_max=50):
"""1^k + 2^k + ... + (m-1)^k = m^k. Only known: k=1, m=3."""
sols = []
for k in range(1, k_max + 1):
for m in range(2, m_max + 1):
s = sum(i ** k for i in range(1, m))
if s == m ** k:
sols.append((k, m))
return sols
@staticmethod
def Lehmer_totient_check(N):
"""Lehmer: phi(n) | n-1 ⇒ n prime? Find counterexamples ≤ N."""
ces = []
for n in range(2, N + 1):
if not isprime(n):
if (n - 1) % totient(n) == 0:
ces.append(n)
return ces
# ============================================================
# OPERATORS — PRIME GAPS
# ============================================================
class PrimeGapOps:
"""Andrica, Cramér, Polignac."""
@staticmethod
def Andrica_max(N):
"""Max √(p_{n+1}) − √(p_n) for primes ≤ N."""
primes = list(primerange(2, N))
max_diff = 0.0
max_at = None
for i in range(len(primes) - 1):
d = math.sqrt(primes[i + 1]) - math.sqrt(primes[i])
if d > max_diff:
max_diff = d
max_at = (primes[i], primes[i + 1])
return max_diff, max_at
@staticmethod
def Cramer_max(N):
"""Max prime gap g_n for primes ≤ N. Conjecture: g_n < (log p_n)²."""
primes = list(primerange(2, N))
max_gap = 0
max_at = None
for i in range(len(primes) - 1):
g = primes[i + 1] - primes[i]
if g > max_gap:
max_gap = g
max_at = primes[i]
return max_gap, max_at
@staticmethod
def Polignac_count(k, N):
"""Number of prime gaps of size k for primes ≤ N."""
primes = list(primerange(2, N))
return sum(1 for i in range(len(primes) - 1)
if primes[i + 1] - primes[i] == k)
# ============================================================
# DB EXTENSION
# ============================================================
EXT_SCHEMA = """
CREATE TABLE IF NOT EXISTS twin_prime_pairs (
p INTEGER PRIMARY KEY,
p_plus_2 INTEGER,
in_anchor_S INTEGER
);
CREATE TABLE IF NOT EXISTS twin_prime_counts (
N INTEGER PRIMARY KEY,
actual INTEGER,
HL_estimate REAL,
Brun_upper REAL,
HL_ratio REAL,
Brun_ratio REAL
);
CREATE TABLE IF NOT EXISTS goldbach_full (
c INTEGER PRIMARY KEY,
n_2c INTEGER,
G_count INTEGER,
HL_estimate REAL,
pair_first TEXT
);
CREATE TABLE IF NOT EXISTS sidon_sets (
N INTEGER PRIMARY KEY,
sidon_size INTEGER,
bound_estimate REAL,
sidon_first10 TEXT
);
CREATE TABLE IF NOT EXISTS ramsey_table (
s INTEGER, k INTEGER,
actual INTEGER,
lower_CJMS REAL,
upper_Shearer REAL,
framework_anchor TEXT,
PRIMARY KEY (s, k)
);
CREATE TABLE IF NOT EXISTS diophantine_results (
problem TEXT, input TEXT, result TEXT, status TEXT,
PRIMARY KEY (problem, input)
);
CREATE TABLE IF NOT EXISTS prime_gaps (
N INTEGER PRIMARY KEY,
Andrica_max_diff REAL,
Andrica_at TEXT,
Cramer_max_gap INTEGER,
Cramer_at INTEGER,
twin_count INTEGER,
cousin_count INTEGER,
sexy_count INTEGER
);
"""
def populate_extensions(db_path, max_N_twin=1_000_000, max_c_goldbach=2000,
max_N_sidon=1000, max_N_andrica=100_000):
"""Populate all extension tables."""
conn = sqlite3.connect(db_path)
cur = conn.cursor()
cur.executescript(EXT_SCHEMA)
conn.commit()
print(f"\n{'='*70}")
print(f"Populating operator extensions in {db_path}")
print(f"{'='*70}")
# Twin prime pairs ≤ S = 214
print("\n[Twin primes] Pairs ≤ 214 (anchor S)")
pairs = TwinPrimeOps.T_hat_pairs(S_ATLAS, max_pairs=100)
cur.executemany(
"INSERT OR REPLACE INTO twin_prime_pairs VALUES (?,?,?)",
[(p, q, 1) for p, q in pairs]
)
print(f" Stored {len(pairs)} twin pairs ≤ {S_ATLAS}")
# Twin prime counts at scales
print("\n[Twin primes] Counts at scales")
scales = [100, 1000, 10000, 100_000, 1_000_000]
for N in scales:
if N > max_N_twin: continue
actual = TwinPrimeOps.T_hat(N)
hl = TwinPrimeOps.HL_hat(N)
brun = TwinPrimeOps.Brun_hat(N)
cur.execute(
"INSERT OR REPLACE INTO twin_prime_counts VALUES (?,?,?,?,?,?)",
(N, actual, hl, brun, actual / hl if hl else 0,
actual / brun if brun else 0)
)
print(f" N={N:>7}: actual={actual:>5}, HL={hl:>10.2f}, ratio={actual/hl:.4f}")
# Goldbach
print("\n[Goldbach] G(2c) for c in [2, max]")
for c in range(2, max_c_goldbach + 1):
n = 2 * c
G_count = GoldbachOps.G_hat(c)
hl_est = GoldbachOps.G_lower_HL(c)
# First pair
first_pair = None
for p in primerange(2, c + 1):
if isprime(n - p):
first_pair = (p, n - p)
break
first_str = f"{first_pair[0]},{first_pair[1]}" if first_pair else None
cur.execute(
"INSERT OR REPLACE INTO goldbach_full VALUES (?,?,?,?,?)",
(c, n, G_count, hl_est, first_str)
)
print(f" Stored {max_c_goldbach - 1} Goldbach entries")
g_S = cur.execute(
"SELECT G_count FROM goldbach_full WHERE c=?", (K,)).fetchone()
if g_S:
print(f" G(214) = {g_S[0]} = 2^N_c = {2**N_C}: anchor verified {g_S[0] == 8}")
# Sidon
print("\n[Sidon] Maximal Sidon subset")
for N in [50, 100, 500, 1000]:
if N > max_N_sidon: continue
S = SidonOps.max_Sidon_in(N)
bnd = SidonOps.Sidon_density_bound(N)
first10 = ",".join(map(str, S[:10]))
cur.execute(
"INSERT OR REPLACE INTO sidon_sets VALUES (?,?,?,?)",
(N, len(S), bnd, first10)
)
print(f" N={N}: |Sidon|={len(S)}, bound √N+N^0.25 = {bnd:.2f}")
# Ramsey
print("\n[Ramsey] R(3, k)")
for k in range(2, 12):
actual = RamseyOps.R_hat(3, k)
lb = RamseyOps.lower_bound_CJMS(k)
ub = RamseyOps.upper_bound_Shearer(k)
anchor = ""
if k == N_C: anchor = f"R(3,N_c) = {actual} = 2·N_c"
elif k == N_ST: anchor = f"R(3,N_st) = {actual} = N_c²"
cur.execute(
"INSERT OR REPLACE INTO ramsey_table VALUES (?,?,?,?,?,?)",
(3, k, actual, lb, ub, anchor)
)
print(f" Stored R(3, k) for k ∈ [2, 11]")
# Diophantine
print("\n[Diophantine] Brocard, Beal, Catalan, Erdős-Moser, Lehmer")
# Brocard up to n=20
for n in range(1, 21):
m, is_sq = DiophantineOps.Brocard_check(n)
cur.execute(
"INSERT OR REPLACE INTO diophantine_results VALUES (?,?,?,?)",
("Brocard", str(n), str(m) if is_sq else "no",
"SOLUTION" if is_sq else "no_square")
)
# Beal — counterexamples
ces = DiophantineOps.Beal_search(20, 5)
cur.execute(
"INSERT OR REPLACE INTO diophantine_results VALUES (?,?,?,?)",
("Beal", "max_base=20,max_exp=5", str(ces),
"VERIFIED_no_counterexample" if not ces else f"COUNTEREXAMPLES:{len(ces)}")
)
# Catalan
sols = DiophantineOps.Catalan_search(50, 7)
cur.execute(
"INSERT OR REPLACE INTO diophantine_results VALUES (?,?,?,?)",
("Catalan", "max_base=50,max_exp=7", str(sols),
"SOLVED_2002" if sols == [(3, 2, 2, 3)] else "UNEXPECTED")
)
# Erdős-Moser
sols = DiophantineOps.Erdos_Moser_check(4, 50)
cur.execute(
"INSERT OR REPLACE INTO diophantine_results VALUES (?,?,?,?)",
("Erdos_Moser", "k≤4,m≤50", str(sols), "VERIFIED_only_(1,3)")
)
# Lehmer
ces = DiophantineOps.Lehmer_totient_check(10000)
cur.execute(
"INSERT OR REPLACE INTO diophantine_results VALUES (?,?,?,?)",
("Lehmer", "N=10000", str(ces),
"VERIFIED_no_counterexample" if not ces else f"COUNTEREXAMPLES:{len(ces)}")
)
print(f" Stored 5 Diophantine families")
# Prime gaps
print("\n[Prime gaps] Andrica, Cramér, Polignac")
for N in [1000, 10000, 100_000]:
if N > max_N_andrica: continue
max_diff, max_at = PrimeGapOps.Andrica_max(N)
max_gap, gap_at = PrimeGapOps.Cramer_max(N)
twin_count = PrimeGapOps.Polignac_count(2, N)
cousin_count = PrimeGapOps.Polignac_count(4, N)
sexy_count = PrimeGapOps.Polignac_count(6, N)
cur.execute(
"INSERT OR REPLACE INTO prime_gaps VALUES (?,?,?,?,?,?,?,?)",
(N, max_diff, str(max_at), max_gap, gap_at,
twin_count, cousin_count, sexy_count)
)
print(f" N={N}: max √-diff={max_diff:.4f} at {max_at}, "
f"max gap={max_gap} at {gap_at}, "
f"twin/cousin/sexy={twin_count}/{cousin_count}/{sexy_count}")
conn.commit()
# Final summary
print(f"\n{'='*70}")
print(f"EXTENSION TABLES SUMMARY")
print(f"{'='*70}")
tables = ["twin_prime_pairs", "twin_prime_counts", "goldbach_full",
"sidon_sets", "ramsey_table", "diophantine_results", "prime_gaps"]
for t in tables:
n = cur.execute(f"SELECT COUNT(*) FROM {t}").fetchone()[0]
print(f" {t:25} {n:>7}")
conn.close()
if __name__ == "__main__":
import sys
db = sys.argv[1] if len(sys.argv) > 1 else "/home/claude/work/erdos_solver/erdos_solver.sqlite"
populate_extensions(db)