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++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std;
// Function to check if a // number is prime or not bool isPrime( int num)
{ if (num <= 1)
return false ;
for ( int i = 2; i * i <= num; i++)
// If a divisor of n exists
if (num % i == 0)
return false ;
return true ;
} // Function to check if a // number is Full Prime or not bool isFulPrime( int n)
{ // If n is not a prime
if (!isPrime(n))
return false ;
// Otherwise
else {
while (n > 0) {
// Extract digit
int rem = n % 10;
// If any digit of n
// is non-prime
if (!(rem == 2 || rem == 3
|| rem == 5 || rem == 7))
return false ;
n = n / 10;
}
}
return true ;
} // Function to print count of // Full Primes in a range [L, R] int countFulPrime( int L, int R)
{ // Stores count of full primes
int cnt = 0;
for ( int i = L; i <= R; i++) {
// Check if i is full prime
if ((i % 2) != 0 && isFulPrime(i)) {
cnt++;
}
}
return cnt;
} // Driver Code int main()
{ int L = 1, R = 100;
// Stores count of full primes
int ans = 0;
if (L < 3)
ans++;
cout << ans + countFulPrime(L, R);
return 0;
} |
// Java program to implement // the above approach import java.io.*;
import java.util.*;
class GFG{
// Function to check if a // number is prime or not static boolean isPrime( int num)
{ if (num <= 1 )
return false ;
for ( int i = 2 ; i * i <= num; i++)
// If a divisor of n exists
if (num % i == 0 )
return false ;
return true ;
} // Function to check if a // number is Full Prime or not static boolean isFulPrime( int n)
{ // If n is not a prime
if (!isPrime(n))
return false ;
// Otherwise
else
{
while (n > 0 )
{
// Extract digit
int rem = n % 10 ;
// If any digit of n
// is non-prime
if (!(rem == 2 || rem == 3 ||
rem == 5 || rem == 7 ))
return false ;
n = n / 10 ;
}
}
return true ;
} // Function to print count of // Full Primes in a range [L, R] static int countFulPrime( int L, int R)
{ // Stores count of full primes
int cnt = 0 ;
for ( int i = L; i <= R; i++)
{
// Check if i is full prime
if ((i % 2 ) != 0 && isFulPrime(i))
{
cnt++;
}
}
return cnt;
} // Driver Code public static void main (String[] args)
{ int L = 1 , R = 100 ;
// Stores count of full primes
int ans = 0 ;
if (L < 3 )
ans++;
System.out.println(ans + countFulPrime(L, R));
} } // This code is contributed by sanjoy_62 |
# Python3 program to implement # the above approach # Function to check if a # number is prime or not def isPrime(num):
if (num < = 1 ):
return False
for i in range ( 2 , num + 1 ):
if i * i > num:
break
# If a divisor of n exists
if (num % i = = 0 ):
return False
return True
# Function to check if a # number is Full Prime or not def isFulPrime(n):
# If n is not a prime
if ( not isPrime(n)):
return False
# Otherwise
else :
while (n > 0 ):
# Extract digit
rem = n % 10
# If any digit of n
# is non-prime
if ( not (rem = = 2 or rem = = 3 or
rem = = 5 or rem = = 7 )):
return False
n = n / / 10
return True
# Function to print count of # Full Primes in a range [L, R] def countFulPrime(L, R):
# Stores count of full primes
cnt = 0
for i in range (L, R + 1 ):
# Check if i is full prime
if ((i % 2 ) ! = 0 and isFulPrime(i)):
cnt + = 1
return cnt
# Driver Code if __name__ = = '__main__' :
L = 1
R = 100
# Stores count of full primes
ans = 0
if (L < 3 ):
ans + = 1
print (ans + countFulPrime(L, R))
# This code is contributed by mohit kumar 29 |
// C# program to implement // the above approach using System;
class GFG{
// Function to check if a // number is prime or not static bool isPrime( int num)
{ if (num <= 1)
return false ;
for ( int i = 2; i * i <= num; i++)
// If a divisor of n exists
if (num % i == 0)
return false ;
return true ;
} // Function to check if a // number is Full Prime or not static bool isFulPrime( int n)
{ // If n is not a prime
if (!isPrime(n))
return false ;
// Otherwise
else
{
while (n > 0)
{
// Extract digit
int rem = n % 10;
// If any digit of n
// is non-prime
if (!(rem == 2 || rem == 3 ||
rem == 5 || rem == 7))
return false ;
n = n / 10;
}
}
return true ;
} // Function to print count of // Full Primes in a range [L, R] static int countFulPrime( int L, int R)
{ // Stores count of full primes
int cnt = 0;
for ( int i = L; i <= R; i++)
{
// Check if i is full prime
if ((i % 2) != 0 && isFulPrime(i))
{
cnt++;
}
}
return cnt;
} // Driver Code public static void Main (String[] args)
{ int L = 1, R = 100;
// Stores count of full primes
int ans = 0;
if (L < 3)
ans++;
Console.WriteLine(ans + countFulPrime(L, R));
} } // This code is contributed by math_lover |
<script> // Javascript Program to implement // the above approach // Function to check if a // number is prime or not function isPrime(num)
{ if (num <= 1)
return false ;
for (let i = 2; i * i <= num; i++)
// If a divisor of n exists
if (num % i == 0)
return false ;
return true ;
} // Function to check if a // number is Full Prime or not function isFulPrime(n)
{ // If n is not a prime
if (!isPrime(n))
return false ;
// Otherwise
else
{
while (n > 0)
{
// Extract digit
let rem = n % 10;
// If any digit of n
// is non-prime
if (!(rem == 2 || rem == 3 ||
rem == 5 || rem == 7))
return false ;
n = Math.floor(n / 10);
}
}
return true ;
} // Function to print count of // Full Primes in a range [L, R] function countFulPrime(L, R)
{ // Stores count of full primes
let cnt = 0;
for (let i = L; i <= R; i++)
{
// Check if i is full prime
if ((i % 2) != 0 && isFulPrime(i))
{
cnt++;
}
}
return cnt;
} // Driver code let L = 1, R = 100; // Stores count of full primes let ans = 0; if (L < 3)
ans++;
document.write(ans + countFulPrime(L, R)); // This code is contributed by splevel62 </script> |
8
Time Complexity: O(N3/2)
Auxiliary Space: O(1)