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 firstname.lastname@example.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.
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.
- Quick ways to check for Prime and find next Prime in Java
- Find coordinates of a prime number in a Prime Spiral
- Prime numbers after prime P with sum S
- Sum of each element raised to (prime-1) % prime
- Print the nearest prime number formed by adding prime numbers to N
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether the sum of prime elements of the array is prime or not
- Print prime numbers with prime sum of digits in an array
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of prime numbers without odd prime digits
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- 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
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Count all prime numbers in a given range whose sum of digits is also prime
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Improved By : jit_t