Right | Java code to check right - truncatable prime or not . ; Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Returns true if n is right - truncatable , else false ; Generating primes using Sieve ; Checking whether the number remains prime when the last ( right ) digit is successively removed ; Driver program
import java . io . * ; class GFG { static void sieveOfEratosthenes ( int n , boolean isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } static boolean rightTruPrime ( int n ) { boolean isPrime [ ] = new boolean [ n + 1 ] ; sieveOfEratosthenes ( n , isPrime ) ; while ( n != 0 ) { if ( isPrime [ n ] ) n = n / 10 ; else return false ; } return true ; } public static void main ( String args [ ] ) { int n = 59399 ; if ( rightTruPrime ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }