Obviously we could brute force all 9 possibilities for the next number on the far left, but at numbers of this size I personally have no tool to check if the resulting number is prime or not.
Well, let's make the problem a little simpler.
Call the integer $n$ "pseudoprime" if it has no prime factors (other than $n$ itself) less than a million. Actual prime numbers are then a subset of pseudoprimes. And all pseudoprimes less than $10^{12}$ are primes. This is easily checked by computer.
import math
def generate_primes(limit):
"""
Return a list of all primes in range(2, limit).
"""
result = []
potential = range(2, limit)
while potential:
p = potential[0]
result.append(p)
potential = [n for n in potential if n % p]
return result
PRIMES = generate_primes(10**6)
def is_pseudoprime(n):
# Special handling of 1, 0, and negatives
if n < 2:
return False
# Test every known prime up to sqrt(n)
max_divisor = int(math.sqrt(n) * 1.00000001)
for p in PRIMES:
if p > max_divisor:
return True # n is a true prime
if n % p == 0:
return False # n is composite
return True # Ran out of trial divisors. Maybe prime.
def is_ltpp(n):
"""
Check if n is a left-truncatable pseudoprime.
"""
num_digits = len(str(n))
return all(is_pseudoprime(n % 10 ** k) for k in range(1, num_digits + 1))
We can now find the left-truncatable pseudoprimes by brute force.
def find_ltpp():
# Start with single-digit primes.
num_digits = 1
ltpp = [2, 3, 5, 7]
while True:
print('There are {0} {1}-digit LTPPs. The largest is {2}.'
.format(len(ltpp), num_digits, ltpp[-1]))
# Add one digit on the left.
potential = [n + digit * 10**num_digits
for n in ltpp
for digit in range(1, 10)]
num_digits += 1
ltpp = [n for n in potential if is_ltpp(n)]
if not ltpp:
print('There are NO {0}-digit LTPPs.'.format(num_digits))
break
If you're sufficiently patient, this function produces the output:
There are 4 1-digit LTPPs. The largest is 7.
There are 11 2-digit LTPPs. The largest is 97.
There are 39 3-digit LTPPs. The largest is 997.
There are 99 4-digit LTPPs. The largest is 6997.
There are 192 5-digit LTPPs. The largest is 96997.
There are 326 6-digit LTPPs. The largest is 496997.
There are 429 7-digit LTPPs. The largest is 6396997.
There are 521 8-digit LTPPs. The largest is 96396997.
There are 545 9-digit LTPPs. The largest is 396396997.
There are 517 10-digit LTPPs. The largest is 4396396997.
There are 448 11-digit LTPPs. The largest is 76633396997.
There are 354 12-digit LTPPs. The largest is 616333396997.
There are 298 13-digit LTPPs. The largest is 5616333396997.
There are 265 14-digit LTPPs. The largest is 35616333396997.
There are 215 15-digit LTPPs. The largest is 435616333396997.
There are 194 16-digit LTPPs. The largest is 6435616333396997.
There are 188 17-digit LTPPs. The largest is 65678739293946997.
There are 158 18-digit LTPPs. The largest is 165678739293946997.
There are 129 19-digit LTPPs. The largest is 6165678739293946997.
There are 118 20-digit LTPPs. The largest is 56165678739293946997.
There are 108 21-digit LTPPs. The largest is 666276812967623946997.
There are 101 22-digit LTPPs. The largest is 9151351291983366421997.
There are 92 23-digit LTPPs. The largest is 33151351291983366421997.
There are 82 24-digit LTPPs. The largest is 233151351291983366421997.
There are 60 25-digit LTPPs. The largest is 6233151351291983366421997.
There are 55 26-digit LTPPs. The largest is 19327957389768645663786197.
There are 53 27-digit LTPPs. The largest is 572334815396334245663786197.
There are 49 28-digit LTPPs. The largest is 9572334815396334245663786197.
There are 42 29-digit LTPPs. The largest is 49572334815396334245663786197.
There are 38 30-digit LTPPs. The largest is 619572334815396334245663786197.
There are 43 31-digit LTPPs. The largest is 5934572334815396334245663786197.
There are 36 32-digit LTPPs. The largest is 45934572334815396334245663786197.
There are 29 33-digit LTPPs. The largest is 757863372334815396334245663786197.
There are 23 34-digit LTPPs. The largest is 6273863372334815396334245663786197.
There are 19 35-digit LTPPs. The largest is 89139326798675469278339997564326947.
There are 21 36-digit LTPPs. The largest is 939139326798675469278339997564326947.
There are 17 37-digit LTPPs. The largest is 2939139326798675469278339997564326947.
There are 12 38-digit LTPPs. The largest is 62939139326798675469278339997564326947.
There are 10 39-digit LTPPs. The largest is 162939139326798675469278339997564326947.
There are 10 40-digit LTPPs. The largest is 6162939139326798675469278339997564326947.
There are 8 41-digit LTPPs. The largest is 12162939139326798675469278339997564326947.
There are 6 42-digit LTPPs. The largest is 512162939139326798675469278339997564326947.
There are 5 43-digit LTPPs. The largest is 9496392127212135769692168751546215769833347.
There are 2 44-digit LTPPs. The largest is 96335316563367861332796686312646216567629137.
There are 3 45-digit LTPPs. The largest is 196335316563367861332796686312646216567629137.
There are 4 46-digit LTPPs. The largest is 5196335316563367861332796686312646216567629137.
There are 1 47-digit LTPPs. The largest is 23616335316563367861332796686312646216567629137.
There are 2 48-digit LTPPs. The largest is 623616335316563367861332796686312646216567629137.
There are 3 49-digit LTPPs. The largest is 9623616335316563367861332796686312646216567629137.
There are 4 50-digit LTPPs. The largest is 49623616335316563367861332796686312646216567629137.
There are 4 51-digit LTPPs. The largest is 649623616335316563367861332796686312646216567629137.
There are 3 52-digit LTPPs. The largest is 7651623616335316563367861332796686312646216567629137.
There are 2 53-digit LTPPs. The largest is 64351623616335316563367861332796686312646216567629137.
There are 1 54-digit LTPPs. The largest is 578121623616335316563367861332796686312646216567629137.
There are NO 55-digit LTPPs.
So, the left-truncatable pseudoprimes thin out until you reach a 54-digit example, and none at all for 55 (or more) digits.
Since left-truncatable primes are a subset of left-truncatable pseudoprimes, and the latter is finite, then it follows that there are only a finite number of left-truncatable primes. To find them, you'll need a way to filter the left-truncatable pseudoprime list to exclude numbers that aren't actually prime.