Find Largest Special Prime which is less than or equal to a given number
Last Updated :
23 May, 2022
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.
Examples:
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:
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; i++) {
if (sieve[i]) {
for ( long long j = i * i; j <= N; j += i) {
sieve[j] = false ;
}
}
}
while ( true ) {
if (checkSpecialPrime(sieve, N)) {
cout << N << '\n' ;
break ;
}
else
N--;
}
}
int main()
{
findSpecialPrime(379);
findSpecialPrime(100);
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 ];
sieve[ 0 ] = sieve[ 1 ] = false ;
for ( int i= 0 ;i<N+ 10 ;i++)
sieve[i]= true ;
for ( int i = 2 ; i <= N; i++) {
if (sieve[i]) {
for ( int j = i * i; j <= N; j += i) {
sieve[j] = false ;
}
}
}
while ( true ) {
if (checkSpecialPrime(sieve, N)) {
System.out.println(N);
break ;
}
else
N--;
}
}
public static void main(String [] args)
{
findSpecialPrime( 379 );
findSpecialPrime( 100 );
}
}
|
Python 3
def checkSpecialPrime(sieve, num):
while (num) :
if ( not sieve[num]) :
return False
num / / = 10
return True
def findSpecialPrime(N):
sieve = [ True ] * (N + 10 )
sieve[ 0 ] = sieve[ 1 ] = False ;
for i in range ( 2 , N + 1 ) :
if (sieve[i]) :
for j in range (i * i, N + 1 , i) :
sieve[j] = False
while ( True ) :
if (checkSpecialPrime(sieve, N)):
print ( N)
break
else :
N - = 1
if __name__ = = "__main__" :
findSpecialPrime( 379 )
findSpecialPrime( 100 )
|
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];
for ( int i = 0; i < N + 10; i++)
sieve[i] = true ;
sieve[0] = sieve[1] = false ;
for ( int i = 2; i <= N; i++) {
if (sieve[i]) {
for ( int j = i * i; j <= N; j += i) {
sieve[j] = false ;
}
}
}
while ( true ) {
if (checkSpecialPrime(sieve, N)) {
Console.WriteLine(N);
break ;
}
else
N--;
}
}
public static void Main()
{
findSpecialPrime(379);
findSpecialPrime(100);
}
}
|
PHP
<?php
function checkSpecialPrime(& $sieve , $num )
{
while ( $num )
{
if (! $sieve [ $num ])
{
return false;
}
$num = (int)( $num / 10);
}
return true;
}
function findSpecialPrime( $N )
{
$sieve = array_fill (0, $N + 10, true);
$sieve [0] = $sieve [1] = false;
for ( $i = 2; $i <= $N ; $i ++)
{
if ( $sieve [ $i ])
{
for ( $j = $i * $i ; $j <= $N ; $j += $i )
{
$sieve [ $j ] = false;
}
}
}
while (true)
{
if (checkSpecialPrime( $sieve , $N ))
{
echo $N . "\n" ;
break ;
}
else
$N --;
}
}
findSpecialPrime(379);
findSpecialPrime(100);
?>
|
Javascript
<script>
function checkSpecialPrime(sieve,num)
{
while (num!=0) {
if (!sieve[num]) {
return false ;
}
num =Math.floor(num/ 10);
}
return true ;
}
function findSpecialPrime(N)
{
let sieve= new Array(N+10);
sieve[0] = sieve[1] = false ;
for (let i=0;i<N+10;i++)
sieve[i]= true ;
for (let i = 2; i <= N; i++) {
if (sieve[i]) {
for ( let j = i * i; j <= N; j += i) {
sieve[j] = false ;
}
}
}
while ( true ) {
if (checkSpecialPrime(sieve, N)) {
document.write(N+ "<br>" );
break ;
}
else
N--;
}
}
findSpecialPrime(379);
findSpecialPrime(100);
</script>
|
Time Complexity: O(N*log(log N)), where N represents the given integer.
Auxiliary Space: O(N), where N represents the given integer.
Share your thoughts in the comments
Please Login to comment...