Given two integers L and R, the task is to count the number of full prime numbers that are present in the given range.
A number is said to be Full prime if the number itself is prime and all its digits are also prime.
Examples:
- 53 is Full Prime because it is prime and all its digits (5 and 3) are also prime.
- 13 is not Full Prime because it has a non-prime digit ( 1 is not prime).
Examples:
Input: L = 1, R = 100
Output : 8
Explanations: 2 3 5 7 23 37 53 73 are the Full Prime numbers between 1 and 100. Therefore, the count is 8.
Input: L = 200, R = 300
Output: 5
Explanation: 223 227 233 257 277 are the Full Prime numbers between 200 and 300. Therefore, the count is 5.
Approach: Follow the steps below to solve the problem:
- Simply traverse the range from L to R.
- For every number i in the range, check if it is divisible by any number from the range [2, sqrt(i)]. If found to be true, then it is not a prime. Proceed to the next number.
- Otherwise, check if all its digits are prime or not. If found to be true, increase count.
- Finally, after complete traversal of the range, print the value of count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int num)
{
if (num <= 1)
return false ;
for ( int i = 2; i * i <= num; i++)
if (num % i == 0)
return false ;
return true ;
}
bool isFulPrime( int n)
{
if (!isPrime(n))
return false ;
else {
while (n > 0) {
int rem = n % 10;
if (!(rem == 2 || rem == 3
|| rem == 5 || rem == 7))
return false ;
n = n / 10;
}
}
return true ;
}
int countFulPrime( int L, int R)
{
int cnt = 0;
for ( int i = L; i <= R; i++) {
if ((i % 2) != 0 && isFulPrime(i)) {
cnt++;
}
}
return cnt;
}
int main()
{
int L = 1, R = 100;
int ans = 0;
if (L < 3)
ans++;
cout << ans + countFulPrime(L, R);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static boolean isPrime( int num)
{
if (num <= 1 )
return false ;
for ( int i = 2 ; i * i <= num; i++)
if (num % i == 0 )
return false ;
return true ;
}
static boolean isFulPrime( int n)
{
if (!isPrime(n))
return false ;
else
{
while (n > 0 )
{
int rem = n % 10 ;
if (!(rem == 2 || rem == 3 ||
rem == 5 || rem == 7 ))
return false ;
n = n / 10 ;
}
}
return true ;
}
static int countFulPrime( int L, int R)
{
int cnt = 0 ;
for ( int i = L; i <= R; i++)
{
if ((i % 2 ) != 0 && isFulPrime(i))
{
cnt++;
}
}
return cnt;
}
public static void main (String[] args)
{
int L = 1 , R = 100 ;
int ans = 0 ;
if (L < 3 )
ans++;
System.out.println(ans + countFulPrime(L, R));
}
}
|
Python3
def isPrime(num):
if (num < = 1 ):
return False
for i in range ( 2 , num + 1 ):
if i * i > num:
break
if (num % i = = 0 ):
return False
return True
def isFulPrime(n):
if ( not isPrime(n)):
return False
else :
while (n > 0 ):
rem = n % 10
if ( not (rem = = 2 or rem = = 3 or
rem = = 5 or rem = = 7 )):
return False
n = n / / 10
return True
def countFulPrime(L, R):
cnt = 0
for i in range (L, R + 1 ):
if ((i % 2 ) ! = 0 and isFulPrime(i)):
cnt + = 1
return cnt
if __name__ = = '__main__' :
L = 1
R = 100
ans = 0
if (L < 3 ):
ans + = 1
print (ans + countFulPrime(L, R))
|
C#
using System;
class GFG{
static bool isPrime( int num)
{
if (num <= 1)
return false ;
for ( int i = 2; i * i <= num; i++)
if (num % i == 0)
return false ;
return true ;
}
static bool isFulPrime( int n)
{
if (!isPrime(n))
return false ;
else
{
while (n > 0)
{
int rem = n % 10;
if (!(rem == 2 || rem == 3 ||
rem == 5 || rem == 7))
return false ;
n = n / 10;
}
}
return true ;
}
static int countFulPrime( int L, int R)
{
int cnt = 0;
for ( int i = L; i <= R; i++)
{
if ((i % 2) != 0 && isFulPrime(i))
{
cnt++;
}
}
return cnt;
}
public static void Main (String[] args)
{
int L = 1, R = 100;
int ans = 0;
if (L < 3)
ans++;
Console.WriteLine(ans + countFulPrime(L, R));
}
}
|
Javascript
<script>
function isPrime(num)
{
if (num <= 1)
return false ;
for (let i = 2; i * i <= num; i++)
if (num % i == 0)
return false ;
return true ;
}
function isFulPrime(n)
{
if (!isPrime(n))
return false ;
else
{
while (n > 0)
{
let rem = n % 10;
if (!(rem == 2 || rem == 3 ||
rem == 5 || rem == 7))
return false ;
n = Math.floor(n / 10);
}
}
return true ;
}
function countFulPrime(L, R)
{
let cnt = 0;
for (let i = L; i <= R; i++)
{
if ((i % 2) != 0 && isFulPrime(i))
{
cnt++;
}
}
return cnt;
}
let L = 1, R = 100;
let ans = 0;
if (L < 3)
ans++;
document.write(ans + countFulPrime(L, R));
</script>
|
Time Complexity: O(N3/2)
Auxiliary Space: O(1)