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
- Smallest subarray of size greater than K with sum greater than a given value
- Highest and Smallest power of K less than and greater than equal to N respectively
- Largest number not greater than N which can become prime after rearranging its digits
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Smallest subarray from a given Array with sum greater than or equal to K
- Smallest number greater than or equal to N divisible by K
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest number greater than Y with sum of digits equal to X
- Smallest number greater than or equal to N using only digits 1 to K
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Smallest power of 4 greater than or equal to N
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Numbers with prime frequencies greater than or equal to k
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Smallest number greater than n that can be represented as a sum of distinct power of k
- Kth prime number greater than N
- Largest number less than N with digit sum greater than the digit sum of N
- Largest number not greater than N all the digits of which are odd
- Least root of given quadratic equation for value greater than equal to K
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.