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.]


  1000000 eratosthenes
 
  [ false swap 
    number$ witheach
      [ char 0 =
        if [ conclude not ] ] ] is haszero      ( n --> b )
 
  [ 10 / ]                      is truncright   ( n --> n )
 
  [ number$ 
    behead drop $->n drop ]     is truncleft    ( n --> n )
 
  [ dup isprime not iff
      [ drop false ] done
    dup haszero iff
      [ drop false ] done
    true swap
    [ truncleft 
      dup 0 > while
      dup isprime not iff
        [ dip not ] done
      again ] drop ]           is ltruncatable ( n --> b )
 
  [ dup isprime not iff
      [ drop false ] done
    dup haszero iff
      [ drop false ] done
    true swap
    [ truncright 
      dup 0 > while
      dup isprime not iff
        [ dip not ] done
      again ] drop ]           is rtruncatable ( n --> b )
 
  say "Left:  "
  1000000 times [ i ltruncatable if [ i echo conclude ] ]
  cr 
  say "Right: "
  1000000 times [ i rtruncatable if [ i echo conclude ] ]
  cr