Project Euler
Problem 37: Truncatable Primes

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.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.


Runtime: 0
Average: 0 Runs: 0
SD: 0 ms
Max: 0
Min: 1000

I went about this by trimming away ineligible numbers from a list of primes I generated. Numbers that started or ended with 1 were no good. Numbers that included even numbers were no good (unless it was a 2 in the first digit). Numbers that included 5 were no good (again, unless it was in the first digit).

Once I had a suitable list of primes I used two functions to test whether they could be truncated to the left and right. If they passed that test I included them in my final sum.

71,979