Given a number N. The task is to find the largest special prime which is less 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 Explanation: 379 can be created as => 3 => 37 => 379 Here, all the numbers ie. 3, 37, 379 are prime. Input : N = 100 Output : 79 Explanation: 79 can be created as => 7 => 79, where both 7, 79 are prime numbers.
Approach: The idea is to use Sieve Of eratosthenes. Build the sieve array up to the number N. Then start iteratively back 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:
Time Complexity: O(N*log(log N))
- Smallest Special Prime which is greater than or equal to a given number
- Sum of largest prime factor of each number less than equal to n
- Find largest prime factor of a number
- Largest number with prime digits
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Largest number that divides x and is co-prime with y
- Largest number less than N whose each digit is prime number
- Largest number less than or equal to N/2 which is coprime to N
- Sum of largest divisible powers of p (a prime number) in a range
- Largest number not greater than N which can become prime after rearranging its digits
- Find coordinates of a prime number in a Prime Spiral
- Largest number smaller than or equal to N divisible by K
- Special prime numbers
- Largest number smaller than or equal to n and digits in non-decreasing order
- Largest proper fraction with sum of numerator and denominator equal to a given number
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.