Many people close to FWPhys are aware of, in possession of, or about to possess, our first STEM merchandise, the Left Truncatable Prime Pencil. Printed on it is the largest base-10 number that remains prime as you remove digits from the left side by means of sharpening the pencil.

It’s common knowledge and there’s a Wikipedia page dedicated to it. No matter, I wish to post my own code used to generate it. There’s no algorithmic elegance, just a regular tree search.
####################
Base = 10
####################
import numpy as np
import math
import time
# Fast
def Prime(n):
if n == 0 or n == 1:
return False
elif n % 2 == 0 and n > 2:
return False
return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
NumberLibrary = []
# Initialization
Dig = 0
# Start the tree by finding the prime first digits
for i in range(Base):
if Prime(i):
NumberLibrary.append(i)
# Iterative tree search
Primality = True
while Primality:
Timestart = time.time()
Dig += 1
print(f'Searching and testing digit #{Dig+1}')
Multiplier = Base ** Dig
NewNumberLibrary = []
for n in NumberLibrary:
for i in range(1,Base):
NewNum = n + i * Multiplier
if Prime(NewNum):
NewNumberLibrary.append(NewNum)
if NewNumberLibrary == []:
Primality = False;
print(f'Failed! Time taken: {time.time()-Timestart:.3g} s.')
print(f"Largest left truncatable prime length is {Dig}")
else:
NumberLibrary = NewNumberLibrary
for k in NumberLibrary:
if Base != 10:
print(f"{np.base_repr(k,base = Base)} (base {Base}) = {k} (base 10)")
if Base == 10:
print(k)
print(f"{len(NumberLibrary)} candidate(s) found. Time taken: {time.time()-Timestart:.3g} s.")
print("\n")
And just for fun, here are some lesser left-truncatable primes in other base systems.
Base | Numbers | Value in Base 10 |
3 | 212 | 23 |
4 | 321223 233323 333323 | 3691 3067 4091 |
5 | 222232 | 7817 |
6 | 14141511414451435 | 4836525320399 |
7 | 6642623 | 817337 |
8 | 313636165537775 | 14005650767869 |
9 | 2486266212 4284484465 2462868287 | 988642271 1676456897 977306389 |
10 | 357686312646216567629137 | – |
11 | CPU Meltdown | |
12 | CPU Meltdown |