<jupyter_start><jupyter_text><jupyter_code>#@title Look at probability of prime of form \<prefix>\<x digits>\[1379\]
from IPython.display import clear_output

!apt install libgmp-dev libmpfr-dev libmpc-dev
!pip install gmpy2

clear_output()
#@title Parameters { run: "auto" }

SIMULATIONS = 100000 #@param {type:"integer"}
num_digits  = 80 #@param {type:"integer"}




import random
import gmpy2
import numpy as np
import math
import matplotlib.pyplot as plt
import seaborn as sns
import time
import tqdm


sns.set()

rand = random.Random(49)

base_rate = 1 / math.log(10 ** (num_digits + 0.5))
base_rate *= 2/1 * 5/4 # We filter these

prefixes = list(range(1, 99+1))
counts = [base_rate] + [0 for p in prefixes]

for simulation in range(SIMULATIONS+1):
  middle = random.randint(10 ** (num_digits-4), 10 ** (num_digits-3) - 1)  
  for p in prefixes:
    end = [1,3,7,9][simulation % 4]

    test = str(p) + str(middle) + str(end)
    assert len(test) == num_digits - 1 * (p <= 9)

    if gmpy2.is_prime(int(test)):
      counts[p] += 1

  if simulation and simulation % 1000 == 0:
    ratio = [counts[i] / simulation for i in range(len(counts))]
    ratio[0] = base_rate
    clear_output()

    print ("After", simulation)
    _ = plt.figure(figsize=(12, 8))
    ax = sns.heatmap(np.reshape(ratio, (10, 10)), annot=True, fmt=".3f", cbar=False)
    plt.show()

print ()
print ("Prime Number Theorum: {:.3}".format(ratio[0]))
print ("Avg: {:.3}".format(sum(ratio)/len(ratio)))
print ("Min: {:.3}".format(min(ratio)))
print ("Max: {:.3}".format(max(ratio)))<jupyter_output>After 100000
{'fraction_non_alphanumeric': 0.16666666666666666, 'fraction_numerical': 0.045454545454545456, 'mean_word_length': 4.1294498381877025, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '64317', 'license': 'Apache-2.0', 'n_tokens_mistral': 664, 'n_tokens_neox': 603, 'n_words': 157, 'path': 'A037274/ProbPrimePrefix.ipynb', 'repo_name': 'sethtroisi/OEIS', 'size': '1584'}