Truncatable Primes
Find the sum of the only eleven primes that are both truncatable from left to right and right to left. Single-digit primes (2, 3, 5, 7) are excluded.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The number \(3797\) has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: \(3797\), \(797\), \(97\), and \(7\). Similarly we can work from right to left: \(3797\), \(379\), \(37\), and \(3\).
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
Problem 37: Truncatable Primes
Mathematical Development
Definitions
Definition 1. Let be a prime with decimal representation where . Define:
- for (right-truncations).
- for (left-truncations).
Definition 2. A prime with digits is right-truncatable if is prime for every . It is left-truncatable if is prime for every . It is (two-sided) truncatable if it is both.
Theoretical Development
Theorem 1 (Digit constraints). Let be a truncatable prime with . Then:
- .
- .
- for all .
Proof.
(1) The right-truncation must be prime, so .
(2) The left-truncation must be prime, so . Suppose . Then is even and (since ), hence is composite — contradiction. Similarly, implies with , contradiction. Therefore .
(3) For , the right-truncation is a prime with digits. The units digit of is . A multi-digit prime cannot end in (it would be divisible by 2 or 5). Hence .
Theorem 2 (Finiteness of right-truncatable primes). The set of right-truncatable primes is finite. In particular, .
Proof. Right-truncatable primes are constructed by a tree-growth process: the roots are , and each node spawns children . Each level has branching factor at most 4. By the prime number theorem, , so the density of primes among -digit numbers is . For sufficiently large , no extensions remain prime, and every branch terminates. Complete enumeration yields exactly 83 right-truncatable primes, the largest being .
Corollary 2.1. The set of truncatable primes is finite, since every truncatable prime is right-truncatable.
Theorem 3 (Complete classification). There are exactly 11 truncatable primes:
Proof. By Theorem 2, all truncatable primes lie within the 83 right-truncatable primes. Testing each for left-truncatability (Definition 2) yields exactly the 11 elements of . Alternatively, a sieve-based exhaustive search over all primes below (which suffices since ) confirms the same set.
Lemma 1 (Verification example). is truncatable.
Proof. Right-truncations: , all prime. Left-truncations: , all prime.
Editorial
We precompute all primes below a search bound with the Sieve of Eratosthenes, then scan the primes from 11 upward. For each prime, we test every right truncation and every left truncation against the sieve table; if both families remain prime, the number is truncatable and contributes to the running sum. The search stops once the known total of eleven truncatable primes has been found.
Pseudocode
Algorithm: Sum of Left- and Right-truncatable Primes
Require: A prime search bound large enough to contain all 11 truncatable primes.
Ensure: The sum of the eleven primes that remain prime under every left and right truncation.
1: Build a sieve and prime lookup table on the search range.
2: Initialize found ← 0 and total ← 0.
3: For each prime p ≥ 11 in increasing order, generate all left truncations and all right truncations of p.
4: If every truncation is prime, update found ← found + 1 and total ← total + p.
5: When found = 11, return total.
Complexity Analysis
Proposition. The algorithm runs in time and space.
Proof. The Sieve of Eratosthenes computes all primes below in time and space. For each of the primes (where ), both truncation checks iterate over at most truncations, each requiring arithmetic and an primality lookup. Total post-sieve work: . The sieve dominates: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
const int LIMIT = 1000000;
vector<bool> is_prime(LIMIT, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i < LIMIT; i++)
if (is_prime[i])
for (int j = i * i; j < LIMIT; j += i)
is_prime[j] = false;
auto isRightTruncatable = [&](int n) -> bool {
while (n >= 10) {
n /= 10;
if (n >= LIMIT || !is_prime[n]) return false;
}
return is_prime[n];
};
auto isLeftTruncatable = [&](int n) -> bool {
int pow = 10;
while (pow < n) {
if (!is_prime[n % pow]) return false;
pow *= 10;
}
return true;
};
int count = 0;
long long total = 0;
for (int i = 11; i < LIMIT && count < 11; i++) {
if (!is_prime[i]) continue;
if (isRightTruncatable(i) && isLeftTruncatable(i)) {
total += i;
count++;
}
}
cout << total << endl;
return 0;
}
"""Project Euler Problem 37: Truncatable Primes"""
def solve():
LIMIT = 1_000_000
is_prime = [True] * LIMIT
is_prime[0] = is_prime[1] = False
for i in range(2, int(LIMIT**0.5) + 1):
if is_prime[i]:
for j in range(i * i, LIMIT, i):
is_prime[j] = False
def is_right_truncatable(n):
while n >= 10:
n //= 10
if not is_prime[n]: return False
return is_prime[n]
def is_left_truncatable(n):
p = 10
while p < n:
if not is_prime[n % p]: return False
p *= 10
return True
total, count = 0, 0
for i in range(11, LIMIT):
if is_prime[i] and is_right_truncatable(i) and is_left_truncatable(i):
total += i
count += 1
if count == 11: break
return total
answer = solve()
assert answer == 748317
print(answer)