user
A truncatable prime is a prime number that when you successively remove digits from one end of the prime, you are left with a new prime number.

Examples

The number **997** is called a *left-truncatable prime* as the numbers **997**, **97**, and **7** are all prime.

The number **7393** is a *right-truncatable prime* as the numbers **7393**, **739**, **73**, and **7** formed by removing digits from its right are also prime.

No zeroes are allowed in truncatable primes.

Task

The task is to find the largest left-truncatable and right-truncatable primes less than one million (base 10 is implied).

Related tasks

* Find largest left truncatable prime in a given base
* Sieve of Eratosthenes

See also

* Truncatable Prime from MathWorld.]
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2024.12

You May Attempt This Online.

```
constant ltp = [2, 3, 5, 7], {
    last if .not;
    [ ((1..9) X~ @$_).grep: &is-prime ]
} ... *;

constant rtp = [2, 3, 5, 7], {
    last if .not;
    [ (@$_ X~ (1,3,7,9)).grep: &is-prime ]
} ... *;

say "Largest ltp < 1e6 = ", ltp[5][*-1];
say "Largest rtp < 1e6 = ", rtp[5][*-1];
say "Largest possible ltp = ", ltp.eager.flat.tail;
say "Largest possible rtp = ", rtp.eager.flat.tail;

```

Output:

```
Largest ltp < 1e6 = 998443
Largest rtp < 1e6 = 739399
Largest possible ltp = 357686312646216567629137
Largest possible rtp = 73939133
```