Right | Program to check whether a given number is 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sieveOfEratosthenes ( int n , bool 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 ; } } } bool rightTruPrime ( int n ) { bool isPrime [ n + 1 ] ; sieveOfEratosthenes ( n , isPrime ) ; while ( n ) { if ( isPrime [ n ] ) n = n / 10 ; else return false ; } return true ; } int main ( ) { int n = 59399 ; if ( rightTruPrime ( n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }