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.
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.
Below is the implementation of the above approach.
113 is left truncatable prime
Worst Case Complexity: O(N*N)
Related Article :
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Print the nearest prime number formed by adding prime numbers to N
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Permutation of first N positive integers such that prime numbers are at prime indices
- Quick ways to check for Prime and find next Prime in Java
- Check if a prime number can be expressed as sum of two Prime Numbers
- Count all prime numbers in a given range whose sum of digits is also prime
- Find coordinates of a prime number in a Prime Spiral
- Print prime numbers with prime sum of digits in an array
- Check whether the sum of prime elements of the array is prime or not
- Sum of each element raised to (prime-1) % prime
- Sum of prime numbers without odd prime digits
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Prime numbers after prime P with sum S
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of the first N Prime numbers
Improved By : jit_t