题目如下:
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.
代码如下:
clear;clc;close all
t = 0;
s = 0;
for p = primes(1e6)
n = ceil(log10(p+1));
if p > 10 && all(isprime(floor(p./10.^(1:n-1)))) && all(isprime(mod(p,10.^(1:n-1))))
t = t + 1;
s = s + p;
if t >= 11
break
end
end
end