Right-Truncatable Prime
Last Updated :
07 Mar, 2022
A Right-truncatable prime is a prime which remains prime when the last (“right”) digit is successively removed. For example, 239 is right-truncatable prime since 239, 23 and 2 are all prime. There are 83 right-truncatable primes.
The task is to check whether the given number (N > 0) is right-truncatable prime or not.
Examples:
Input: 239
Output: Yes
Input: 101
Output: No
101 is not right-truncatable prime because
numbers formed are 101, 10 and 1. Here, 101
is prime but 10 and 1 are not prime.
The idea is to generate all the primes less than or equal to the given number N using Sieve of Eratosthenes. Once we have generated all such primes, then we check whether the number remains prime when the last (“right”) digit is successively removed.
C++
#include<bits/stdc++.h>
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;
}
|
Java
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" );
}
}
|
Python3
def sieveOfEratosthenes(n,isPrime) :
isPrime[ 0 ] = isPrime[ 1 ] = False
for i in range ( 2 , n + 1 ) :
isPrime[i] = True
p = 2
while (p * p < = n) :
if (isPrime[p] = = True ) :
i = p * 2
while (i < = n) :
isPrime[i] = False
i = i + p
p = p + 1
def rightTruPrime(n) :
isPrime = [ None ] * (n + 1 )
sieveOfEratosthenes(n, isPrime)
while (n ! = 0 ) :
if (isPrime[n]) :
n = n / / 10
else :
return False
return True
n = 59399
if (rightTruPrime(n)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static void 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 ;
}
}
}
static bool rightTruPrime( int n)
{
bool [] isPrime = new bool [n + 1];
sieveOfEratosthenes(n, isPrime);
while (n != 0) {
if (isPrime[n])
n = n / 10;
else
return false ;
}
return true ;
}
public static void Main()
{
int n = 59399;
if (rightTruPrime(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function sieveOfEratosthenes( $n , & $isPrime )
{
$isPrime [0] = $isPrime [1] = false;
for ( $p = 2; $p * $p <= $n ; $p ++)
{
if ( $isPrime [ $p ] == true)
{
for ( $i = $p * 2; $i <= $n ; $i += $p )
$isPrime [ $i ] = false;
}
}
}
function rightTruPrime( $n )
{
$isPrime = array_fill (0, $n + 1, true);
sieveOfEratosthenes( $n , $isPrime );
while ( $n )
{
if ( $isPrime [ $n ])
$n = (int)( $n / 10);
else
return false;
}
return true;
}
$n = 59399;
if (rightTruPrime( $n ))
echo "Yes\n" ;
else
echo "No\n" ;
?>
|
Javascript
<script>
function sieveOfEratosthenes(n, isPrime)
{
isPrime[0] = isPrime[1] = false ;
for (let i = 2; i <= n; i++)
isPrime[i] = true ;
for (let p = 2; p * p <= n; p++) {
if (isPrime[p] == true ) {
for (let i = p * 2; i <= n; i += p)
isPrime[i] = false ;
}
}
}
function rightTruPrime(n)
{
let isPrime = new Array(n + 1).fill( false );
sieveOfEratosthenes(n, isPrime);
while (n != 0) {
if (isPrime[n])
n = parseInt(n / 10);
else
return false ;
}
return true ;
}
var n = 59399;
if (rightTruPrime(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output:
Yes
Related Article:Left-Truncatable Prime
References:
https://en.wikipedia.org/wiki/Truncatable_prime
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...