Given a number N. The task is to find the smallest special prime which is greater than or equal to N.
A special prime is a number which can be created by placing digits one after another such the all the resulting numbers are prime.
Input: N = 379 Output: 379 379 can be created as => 3 => 37 => 379 Here, all the numbers ie. 3, 37, 379 are prime. Input:N = 100 Output: 233
Approach: The idea is to use Sieve Of Eratosthenes. Build the sieve array up to the number N*10 (Assuming the number will exist in that range). Then start iteratively from the number N checking if the number is prime. If it is prime then check if it is special prime or not.
Now, to check if a number is a special prime or not. Keep dividing the number by 10 and at each point check whether the remaining number is prime or not, which we can do by referring our Sieve array which we have built.
Below is the implementation of the above approach:
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.
- Find Largest Special Prime which is less than or equal to a given number
- Largest number not greater than N which can become prime after rearranging its digits
- Numbers with prime frequencies greater than or equal to k
- Kth prime number greater than N
- Sum of largest prime factor of each number less than equal to n
- Count number of integers less than or equal to N which has exactly 9 divisors
- Largest number less than or equal to N/2 which is coprime to N
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Special prime numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Print all prime numbers less than or equal to N
- Print all Semi-Prime Numbers less than or equal to N
- Sum of Semi-Prime Numbers less than or equal to N
- Queries for the smallest and the largest prime number of given digit
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Nearest prime less than given number n
- Largest number less than N whose each digit is prime number
- Number which has the maximum number of distinct prime factors in the range M to N
- Count of prime digits of a Number which divides the number
- Count all prime numbers in a given range whose sum of digits is also prime
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.