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 = 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:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkSpecialPrime( bool * sieve, int num)
{
while (num) {
if (!sieve[num]) {
return false ;
}
num /= 10;
}
return true ;
}
void findSpecialPrime( int N)
{
bool sieve[N*10];
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 ;
}
}
}
while ( true ) {
if (checkSpecialPrime(sieve, N)) {
cout << N << '\n' ;
break ;
}
else
N++;
}
}
int main()
{
int N = 379;
findSpecialPrime(N);
N = 100;
findSpecialPrime(N);
return 0;
}
|
Java
class GFG
{
static boolean checkSpecialPrime( boolean []sieve, int num)
{
while (num > 0 )
{
if (sieve[num])
{
return false ;
}
num /= 10 ;
}
return true ;
}
static void findSpecialPrime( int N)
{
boolean [] sieve = new boolean [N * 10 + 1 ];
sieve[ 0 ] = sieve[ 1 ] = true ;
for ( int i = 2 ; i <= N * 10 ; i++)
{
if (!sieve[i])
{
for ( int j = i * i; j <= N * 10 ; j += i)
{
sieve[j] = true ;
}
}
}
while ( true )
{
if (checkSpecialPrime(sieve, N))
{
System.out.println(N);
break ;
}
else
N++;
}
}
public static void main(String[] args)
{
int N = 379 ;
findSpecialPrime(N);
N = 100 ;
findSpecialPrime(N);
}
}
|
Python3
def checkSpecialPrime(sieve, num):
while (num):
if (sieve[num] = = False ):
return False
num = int (num / 10 )
return True
def findSpecialPrime(N):
sieve = [ True for i in range (N * 10 + 1 )]
sieve[ 0 ] = False
sieve[ 1 ] = False
for i in range ( 2 , N * 10 + 1 ):
if (sieve[i]):
for j in range (i * i, N * 10 + 1 , i):
sieve[j] = False
while ( True ):
if (checkSpecialPrime(sieve, N)):
print (N)
break
else :
N + = 1
if __name__ = = '__main__' :
N = 379
findSpecialPrime(N)
N = 100
findSpecialPrime(N)
|
C#
using System;
class GFG
{
static bool checkSpecialPrime( bool []sieve, int num)
{
while (num > 0)
{
if (sieve[num])
{
return false ;
}
num /= 10;
}
return true ;
}
static void findSpecialPrime( int N)
{
bool [] sieve = new bool [N * 10 + 1];
sieve[0] = sieve[1] = true ;
for ( int i = 2; i <= N * 10; i++)
{
if (!sieve[i])
{
for ( int j = i * i; j <= N * 10; j += i)
{
sieve[j] = true ;
}
}
}
while ( true )
{
if (checkSpecialPrime(sieve, N))
{
Console.WriteLine(N);
break ;
}
else
N++;
}
}
static void Main()
{
int N = 379;
findSpecialPrime(N);
N = 100;
findSpecialPrime(N);
}
}
|
PHP
<?php
function checkSpecialPrime( $sieve , $num )
{
while ( $num )
{
if (! $sieve [ $num ])
{
return false;
}
$num = floor ( $num / 10);
}
return true;
}
function findSpecialPrime( $N )
{
$sieve = array_fill (0, $N * 10, true);
$sieve [0] = $sieve [1] = false;
for ( $i = 2; $i <= $N * 10; $i ++)
{
if ( $sieve [ $i ])
{
for ( $j = $i * $i ;
$j <= $N * 10; $j += $i )
{
$sieve [ $j ] = false;
}
}
}
while (true)
{
if (checkSpecialPrime( $sieve , $N ))
{
echo $N , "\n" ;
break ;
}
else
$N ++;
}
}
$N = 379;
findSpecialPrime( $N );
$N = 100;
findSpecialPrime( $N );
?>
|
Javascript
<script>
function checkSpecialPrime(sieve , num)
{
while (num > 0)
{
if (sieve[num])
{
return false ;
}
num = parseInt(num / 10);
}
return true ;
}
function findSpecialPrime(N)
{
var sieve = Array.from({length: N * 10 + 1}, (_, i) => false );
sieve[0] = true ;
sieve[1] = true ;
var i = 0, j = 0;
for (i = 2; i <= N * 10; i++)
{
if (!sieve[i])
{
for (j = i * i; j <= N * 10; j += i)
{
sieve[j] = true ;
}
}
}
while ( true )
{
if (checkSpecialPrime(sieve, N))
{
document.write(N+ "<br>" );
break ;
}
else
N++;
}
}
var N = 379;
findSpecialPrime(N);
N = 100;
findSpecialPrime(N);
</script>
|
Time Complexity: O(nlog(logn))
Auxiliary Space: O(n)
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...