# Smallest Special Prime which is greater than or equal to a given number

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.

**Examples:**

Input:N = 379Output:379 379 can be created as => 3 => 37 => 379 Here, all the numbers ie. 3, 37, 379 are prime.Input:N = 100Output: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:

`// CPP program to find the Smallest Special Prime ` `// which is greater than or equal to a given number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check whether the number ` `// is a special prime or not ` `bool` `checkSpecialPrime(` `bool` `* sieve, ` `int` `num) ` `{ ` ` ` `// While number is not equal to zero ` ` ` `while` `(num) { ` ` ` `// If the number is not prime ` ` ` `// return false. ` ` ` `if` `(!sieve[num]) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Else remove the last digit ` ` ` `// by dividing the number by 10. ` ` ` `num /= 10; ` ` ` `} ` ` ` ` ` `// If the number has become zero ` ` ` `// then the number is special prime, ` ` ` `// hence return true ` ` ` `return` `true` `; ` `} ` ` ` `// Function to find the Smallest Special Prime ` `// which is greater than or equal to a given number ` `void` `findSpecialPrime(` `int` `N) ` `{ ` ` ` `bool` `sieve[N*10]; ` ` ` ` ` `// Initially all numbers are considered Primes. ` ` ` `memset` `(sieve, ` `true` `, ` `sizeof` `(sieve)); ` ` ` `sieve[0] = sieve[1] = ` `false` `; ` ` ` `for` `(` `long` `long` `i = 2; i <= N*10; i++) { ` ` ` `if` `(sieve[i]) { ` ` ` ` ` `for` `(` `long` `long` `j = i * i; j <= N*10; j += i) { ` ` ` `sieve[j] = ` `false` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// There is always an answer possible ` ` ` `while` `(` `true` `) { ` ` ` `// Checking if the number is a ` ` ` `// special prime or not ` ` ` `if` `(checkSpecialPrime(sieve, N)) { ` ` ` `// If yes print the number ` ` ` `// and break the loop. ` ` ` `cout << N << ` `'\n'` `; ` ` ` `break` `; ` ` ` `} ` ` ` `// Else increment the number. ` ` ` `else` ` ` `N++; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 379; ` ` ` ` ` `findSpecialPrime(N); ` ` ` ` ` `N = 100; ` ` ` `findSpecialPrime(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

379 233

## Recommended Posts:

- Find Largest Special Prime which is less than or equal to a given number
- Smallest number greater than or equal to N divisible by K
- Numbers with prime frequencies greater than or equal to k
- Kth prime number greater than N
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Smallest prime divisor of a number
- Smallest prime number missing in an array
- Check if the first and last digit of the smallest number forms a prime
- Smallest number k such that the product of digits of k is equal to n
- Sum of largest prime factor of each number less than equal to n
- Special prime numbers
- Smallest integer greater than n such that it consists of digit m exactly k times
- Longest subarray having average greater than or equal to x
- Next smallest prime palindrome
- Sum and product of k smallest and k largest prime numbers in the array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.