import math
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter as FF
import numpy as np
import pandas as pd
import seaborn as sns
#Check if a number is prime.
def primeCheck(n) :
for f in range(2, math.ceil(math.sqrt(n)) + 1):
if n % f == 0:
return(False)
return(True)
#Get all possible prime numbers in a range.
def possibles(l, u):
primes = []
temp = [item for sublist in [(1+10*n, 3+10*n, 7+10*n, 9+10*n) for n in range(l, u)] for item in sublist]
for p in temp:
if primeCheck(p):
primes.append(p)
return(primes)
#Determine pairs of primes given a table and ladder size.
def getPair(digit, digit_df, size) :
for p1 in digit:
for p2 in digit:
if digit_df.at[p1, p2] == size:
print(p1, p2)
def distHelper(digits, digit_df) :
data2 = [x for x in [item for sublist in digit_df.values.tolist() for item in sublist] if not math.isnan(x)]
steps = set(data2)
#Plot.
sns.set()
fig = plt.figure(figsize = (14, 8))
ax = fig.add_subplot()
#Histogram.
bars = plt.hist(x = data2,
#Number of bins.
bins = np.arange(len(steps) + 1) + min(steps) - 0.5,
align = "mid",
color = '#1F968B')
#Labels.
for i in range(len(steps)):
plt.text(bars[1][i] + 0.5,
bars[0][i],
format(int(bars[0][i]), ','),
ha = 'center',
size = 'xx-large',
weight = 'bold',
c = '#481567')
#Title.
ax.set_title("Distribution of Optimal Prime Ladders for " + digits + "-Digit Primes", fontsize = 24)
#Axes.
ax.set_xlabel("Length of Prime Ladders", fontsize = 18)
ax.set_ylabel("Frequency", fontsize = 18)
ax.get_yaxis().set_major_formatter(FF(lambda x, p: format(int(x), ',')))
ax.tick_params(axis = 'both', labelsize = 16)
plt.xticks(ticks = list(steps), labels = list(steps))
fig.savefig("2022.05.06 Classic" + digits + ".png");
two_digit = possibles(1, 10)
df2 = pd.DataFrame(columns = two_digit,
index = two_digit)
all = len(two_digit)
for x in range(all):
m = two_digit[x]
m1 = m % 10
m10 = m // 10
for y in range(all):
n = two_digit[y]
n1 = n % 10
n10 = n // 10
if x == y:
df2.at[m, n] = 1
#Tens different.
elif m1 == n1:
df2.at[m, n] = 2
#Ones different.
elif m10 == n10:
df2.at[m, n] = 2
for s1 in range(3,5):
for s2 in range(2, s1):
for x in df2.index:
for y1 in df2.columns:
#s2 steps.
if df2.at[x, y1] == s2:
#s1-s2 steps from s2 steps is s1 steps.
for y2 in df2.columns:
if (df2.at[y1, y2] == 1+s1-s2) & (math.isnan(df2.at[x, y2])):
df2.at[x, y2] = s1
distHelper("Two", df2)
three_digit = possibles(10, 100)
df3 = pd.DataFrame(columns = three_digit,
index = three_digit)
all = len(three_digit)
for x in range(all):
m = three_digit[x]
m1 = m % 10
m10 = (m // 10) % 10
m100 = m // 100
for y in range(all):
n = three_digit[y]
n1 = n % 10
n10 = (n // 10) % 10
n100 = n // 100
if m == n :
df3.at[m, n] = 1
#Hundreds different.
elif (m10 == n10) & (m1 == n1):
df3.at[m, n] = 2
#Tens different.
elif (m100 == n100) & (m1 == n1):
df3.at[m, n] = 2
#Ones different.
elif (m100 == n100) & (m10 == n10) :
df3.at[m, n] = 2
for s1 in range(3, 8):
for s2 in range(2, s1):
for x in df3.index:
for y1 in df3.columns:
#s2 steps.
if df3.at[x, y1] == s2:
#s1-s2 steps from s2 steps is s1 steps.
for y2 in df3.columns:
if (df3.at[y1, y2] == 1+s1-s2) & (math.isnan(df3.at[x, y2])):
df3.at[x, y2] = s1
print(s1)
3 4 5 6 7
distHelper("Three", df3)
getPair(three_digit, df3, 7)
389 761 761 389
four_digit = possibles(100, 1000)
df4 = pd.DataFrame(columns = four_digit,
index = four_digit)
all = len(four_digit)
for x in range(all):
m = four_digit[x]
m1 = m % 10
m10 = (m // 10) % 10
m100 = (m // 100) % 100
m1000 = m // 1000
for y in range(all):
n = four_digit[y]
n1 = n % 10
n10 = (n // 10) % 10
n100 = (n // 100) % 100
n1000 = n // 1000
if m == n :
df4.at[m, n] = 1
#Thousands different.
elif (m100 == n100) & (m10 == n10) & (m1 == n1):
df4.at[m, n] = 2
#Hundreds different.
elif (m1000 == n1000) & (m10 == n10) & (m1 == n1):
df4.at[m, n] = 2
#Tens different.
elif (m1000 == n1000) & (m100 == n100) & (m1 == n1):
df4.at[m, n] = 2
#Ones different.
elif (m1000 == n1000) & (m100 == n100) & (m10 == n10):
df4.at[m, n] = 2
for s1 in range(3, 13):
for s2 in range(2, s1):
for x in df4.index:
for y1 in df4.columns:
#s2 steps.
if df4.at[x, y1] == s2:
#s1-s2 steps from s2 steps is s1 steps.
for y2 in df4.columns:
if (df4.at[y1, y2] == 1+s1-s2) & (math.isnan(df4.at[x, y2])):
df4.at[x, y2] = s1
print(s1)
3 4 5 6 7 8 9 10 11 12
distHelper("Four", df4)
getPair(four_digit, df4, 9)
6841 6983 6983 6841 7019 7433 7039 7433 7039 7963 7433 7019 7433 7039 7963 7039 9323 9547 9547 9323
for x in range(all):
m = four_digit[x]
m1 = m % 10
m10 = (m // 10) % 10
m100 = (m // 100) % 100
m1000 = m // 1000
for y in range(all):
n = four_digit[y]
n1 = n % 10
n10 = (n // 10) % 10
n100 = (n // 100) % 100
n1000 = n // 1000
if m == n :
df4.at[m, n] = 1
#Thousands different.
if (m1000 != n1000) & (m100 == n100) & (m10 == n10) & (m1 == n1):
print(m,n)
df4.at[m, n] = 2
There are no two four digit primes which differ only by their thousands digit.
five_digit = possibles(1000, 10000)
df5 = pd.DataFrame(columns = five_digit,
index = five_digit)
all = len(five_digit)
for x in range(all):
m = five_digit[x]
m1 = m % 10
m10 = (m // 10) % 10
m100 = (m // 100) % 100
m1000 = m // 1000 % 1000
m10000 = m // 10000
for y in range(all):
n = five_digit[y]
n1 = n % 10
n10 = (n // 10) % 10
n100 = (n // 100) % 100
n1000 = n // 1000 % 1000
n10000 = n // 10000
if m == n :
df5.at[m, n] = 1
#Ten Thousands different.
elif (m1000 == n1000) & (m100 == n100) & (m10 == n10) & (m1 == n1):
print(m, n)
df5.at[m, n] = 2
There are no two five digit primes which differ only by their ten thousands' digit.
six_digit = possibles(1000, 100000)
df6 = pd.DataFrame(columns = six_digit,
index = six_digit)
all = len(six_digit)
for x in range(all):
m = six_digit[x]
m1 = m % 10
m10 = (m // 10) % 10
m100 = (m // 100) % 100
m1000 = m // 1000 % 1000
m10000 = m // 10000 % 10000
m100000 = m // 100000
for y in range(all):
n = six_digit[y]
n1 = n % 10
n10 = (n // 10) % 10
n100 = (n // 100) % 100
n1000 = n // 1000 % 1000
n10000 = n // 10000
n100000 = n // 100000
if m == n :
df6.at[m, n] = 1
#Hundred Thousands different.
elif (m10000 == n10000) & (m1000 == n1000) & (m100 == n100) & (m10 == n10) & (m1 == n1):
print(m, n)
df6.at[m, n] = 2
There are no two six digit primes which differ only by their hundred thousands' digit.