Left-Truncatable Prime

2

A Left-truncatable prime is a prime which in a given base (say 10) does not contain 0 and which remains prime when the leading (“left”) digit is successively removed. For example, 317 is left-truncatable prime since 317, 17 and 7 are all prime. There are total 4260 left-truncatable primes.

The task is to check whether the given number (N >0) is left-truncatable prime or not.

Examples:

Input: 317
Output: Yes

Input: 293
Output: No
293 is not left-truncatable prime because 
numbers formed are 293, 93 and 3. Here, 293 
and 3 are prime but 93 is not prime.

The idea is to first check whether the number contains 0 as a digit or not and count number of digits in the given number N. If it contains 0, then return false otherwise 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 leading (“left”) digit is successively removed.

// Program to check whether a given number
// is left-truncatable prime or not.
#include<bits/stdc++.h>
using namespace std;

/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

// Generate all prime numbers less than n.
bool sieveOfEratosthenes(int n, bool isPrime[])
{
    // 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];
    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] is not changed, then it is
        // a prime
        if (isPrime[p] == true)
        {
            // Update all multiples of p
            for (int i=p*2; i<=n; i += p)
                isPrime[i] = false;
        }
    }
}

// Returns true if n is right-truncatable, else false
bool leftTruPrime(int n)
{
    int temp = n, cnt = 0, temp1;

    // Counting number of digits in the
    // input number and checking whether it
    // contains 0 as digit or not.
    while (temp)
    {
        cnt++; // counting number of digits.

        temp1 = temp%10; // checking whether digit is 0 or not
        if (temp1==0)
            return false; // if digit is 0, return false.
        temp = temp/10;
    }

    // Generating primes using Sieve
    bool isPrime[n+1];
    sieveOfEratosthenes(n, isPrime);

    // Checking whether the number remains prime
    // when the leading ("left") digit is successively
    // removed
    for (int i=cnt; i>0; i--)
    {
        // Checking number by successively removing
        // leading ("left") digit.
        /* n=113, cnt=3
        i=3 mod=1000 n%mod=113
        i=2 mod=100 n%mod=13
        i=3 mod=10 n%mod=3 */
        int mod=  power(10,i);

        if (!isPrime[n%mod]) // checking prime
            return false; // if not prime, return false
    }
    return true; // if remains prime, return true
}

// Driver program
int main()
{
    int n = 113;

    if (leftTruPrime(n))
        cout << n << " is left truncatable prime" << endl;
    else
        cout << n << " is not left truncatable prime" << endl;
    return 0;
}

Output:

113 is left truncatable prime

Worst Case Complexity: O(N*N)

Related Article :
Right-Truncatable Prime

References: https://en.wikipedia.org/wiki/Truncatable_prime

This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.