Count prime numbers in range [L, R] whose single digit sum is also prime
Last Updated :
28 Apr, 2023
Given two integers L and R. The task is to count the prime numbers in the range [L, R], whose single sum is also a prime number.
A single sum is obtained by adding the digits of a number until a single digit is left.
Examples
Input: L = 5, R = 20
Output: 3
Explanation: Prime numbers in the range L = 5 to R = 20 are {5, 7, 11, 13, 17, 19}
Their “single sum” of digits is {5, 7, 2, 4, 8, 1}.
Only {5, 7, 2} are prime. Hence the answer is 3.
Input: L = 1, R = 10
Output: 4
Explanation: Prime numbers in the range L = 1 to R = 10 are {2, 3, 5, 7}.
Their “single sum” of digits is {2, 3, 5, 7}.
Since all the numbers are prime, hence the answer is 4.
Approach: The naive approach is to iterate for each number in the range [L, R] and check if the number is prime or not. If the number is prime, find the single sum of its digits and again check whether the single sum is prime or not. If the single sum is prime, then increment the counter and print the current element in the range [L, R].
Below is the implementation of the above approach.
C++14
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i <= sqrt (n); i++)
if (n % i == 0)
return false ;
return true ;
}
int SingleDigitSum( int & n)
{
if (n <= 9)
return n;
return (n % 9 == 0) ? 9 : n % 9;
}
int countSingleDigitPrimes( int l, int r)
{
int count = 0, i;
for (i = l; i <= r; i++) {
if (isPrime(i)
&& isPrime(SingleDigitSum(i))) {
count++;
}
}
return count;
}
int main()
{
int L = 1, R = 10;
cout << countSingleDigitPrimes(L, R);
return 0;
}
|
Java
class GFG
{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ; i <= Math.sqrt(n); i++)
if (n % i == 0 )
return false ;
return true ;
}
static int SingleDigitSum( int n)
{
if (n <= 9 )
return n;
return (n % 9 == 0 ) ? 9 : n % 9 ;
}
static int countSingleDigitPrimes( int l, int r)
{
int count = 0 , i;
for (i = l; i <= r; i++) {
if (isPrime(i)
&& isPrime(SingleDigitSum(i))) {
count++;
}
}
return count;
}
public static void main(String args[])
{
int L = 1 , R = 10 ;
System.out.println(countSingleDigitPrimes(L, R));
}
}
|
Python3
import math
def isPrime(n):
if n < = 1 :
return False
for i in range ( 2 , math.floor(math.sqrt(n)) + 1 ):
if n % i = = 0 :
return False
return True
def SingleDigitSum(n):
if n < = 9 :
return n
return 9 if (n % 9 = = 0 ) else n % 9
def countSingleDigitPrimes(l, r):
count = 0
i = None
for i in range (l, r + 1 ):
if isPrime(i) and isPrime(SingleDigitSum(i)):
count + = 1
return count
L = 1
R = 10
print (countSingleDigitPrimes(L, R))
|
C#
using System;
class GFG
{
static bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i <= Math.Sqrt(n); i++)
if (n % i == 0)
return false ;
return true ;
}
static int SingleDigitSum( int n)
{
if (n <= 9)
return n;
return (n % 9 == 0) ? 9 : n % 9;
}
static int countSingleDigitPrimes( int l, int r)
{
int count = 0, i;
for (i = l; i <= r; i++) {
if (isPrime(i)
&& isPrime(SingleDigitSum(i))) {
count++;
}
}
return count;
}
public static void Main()
{
int L = 1, R = 10;
Console.Write(countSingleDigitPrimes(L, R));
}
}
|
Javascript
<script>
const isPrime = (n) => {
if (n <= 1)
return false ;
for (let i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
return false ;
return true ;
}
const SingleDigitSum = (n) => {
if (n <= 9)
return n;
return (n % 9 == 0) ? 9 : n % 9;
}
const countSingleDigitPrimes = (l, r) => {
let count = 0, i;
for (i = l; i <= r; i++) {
if (isPrime(i)
&& isPrime(SingleDigitSum(i))) {
count++;
}
}
return count;
}
let L = 1, R = 10;
document.write(countSingleDigitPrimes(L, R));
</script>
|
Time Complexity: O((R – L)*N^(1/2)) where N is the prime number in the range [L, R].
Auxiliary Space: O(1)
Another method:
The approach for finding single-digit primes within a given range is to use the following observations:
- The only single-digit prime numbers are 2, 3, 5, and 7.
- The sum of the digits of a number is congruent to the number modulo 9.
- If a number is not divisible by 3, then its sum of digits is not divisible by 3.
- If a number is not divisible by 2 or 5, then its sum of digits is not divisible by 2 or 5.
Based on these observations, we can take the following approach:
- Check if the range includes any of the single-digit prime numbers (2, 3, 5, or 7) and count them.
- For each number in the range that is not a single-digit prime number, check if it is divisible by 2, 3, 5, or 7. If it is, skip it. Otherwise, calculate the sum of its digits and check if it is a single-digit prime number. If it is, increment the count.
- Return the count as the result.
Algorithm:
- Declare the ‘countSingleDigitPrimes‘ function that will find the count of single-digit prime numbers in the given range.
- Initialize a variable ‘count‘ to 0, which will keep track of the count of single-digit prime numbers in the range.
- In the first for loop, iterate over numbers 2 to 7 (inclusive), and check if they are prime and if they lie in the given range. If they are both prime and in the range, increment the ‘count’ variable.
- In the second for loop, iterate over numbers in the range that are greater than or equal to 8. For each number, check if it is divisible by 2, 3, 5, or 7, and continue to the next number if any of these conditions are true.
- Calculate the sum of digits of the current number, and if it is 0, 1, 4, 7, or 9, continue to the next number.
- Check if both the current number and its sum of digits are prime, and increment the ‘count’ variable if this condition is true.
- Return the ‘count’ variable as the output of the ‘countSingleDigitPrimes’ function.
Below is the implementation of the above code:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i <= sqrt (n); i++)
if (n % i == 0)
return false ;
return true ;
}
int countSingleDigitPrimes( int l, int r)
{
int count = 0;
for ( int i = 2; i <= 7 && i <= r; i++) {
if (l <= i && isPrime(i)) {
count++;
}
}
for ( int i = max(l, 8); i <= r; i++) {
if (i % 2 == 0 || i % 5 == 0) {
continue ;
}
if (i % 3 == 0 || i % 7 == 0) {
continue ;
}
int sum = i % 9;
if (sum == 0 || sum == 1 || sum == 4 || sum == 7 || sum == 9) {
continue ;
}
if (isPrime(i) && isPrime(sum)) {
count++;
}
}
return count;
}
int main()
{
int L = 1, R = 10;
cout << countSingleDigitPrimes(L, R);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ; i <= Math.sqrt(n); i++)
if (n % i == 0 )
return false ;
return true ;
}
public static int countSingleDigitPrimes( int l, int r)
{
int count = 0 ;
for ( int i = 2 ; i <= 7 && i <= r; i++) {
if (l <= i && isPrime(i)) {
count++;
}
}
for ( int i = Math.max(l, 8 ); i <= r; i++) {
if (i % 2 == 0 || i % 5 == 0 ) {
continue ;
}
if (i % 3 == 0 || i % 7 == 0 ) {
continue ;
}
int sum = i % 9 ;
if (sum == 0 || sum == 1 || sum == 4 || sum == 7
|| sum == 9 ) {
continue ;
}
if (isPrime(i) && isPrime(sum)) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int L = 1 , R = 10 ;
System.out.println(countSingleDigitPrimes(L, R));
}
}
|
Python3
import math
def isPrime(n):
if n < = 1 :
return False
for i in range ( 2 , int (math.sqrt(n)) + 1 ):
if n % i = = 0 :
return False
return True
def countSingleDigitPrimes(l, r):
count = 0
for i in range ( 2 , 8 ):
if l < = i < = r and isPrime(i):
count + = 1
for i in range ( max (l, 8 ), r + 1 ):
if i % 2 = = 0 or i % 5 = = 0 :
continue
if i % 3 = = 0 or i % 7 = = 0 :
continue
sum = i % 9
if sum = = 0 or sum = = 1 or sum = = 4 or sum = = 7 or sum = = 9 :
continue
if isPrime(i) and isPrime( sum ):
count + = 1
return count
L, R = 1 , 10
print (countSingleDigitPrimes(L, R))
|
C#
using System;
public class GFG {
public static Boolean isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i <= Math.Sqrt(n); i++)
if (n % i == 0)
return false ;
return true ;
}
public static int countSingleDigitPrimes( int l, int r)
{
int count = 0;
for ( int i = 2; i <= 7 && i <= r; i++) {
if (l <= i && isPrime(i)) {
count++;
}
}
for ( int i = Math.Max(l, 8); i <= r; i++) {
if (i % 2 == 0 || i % 5 == 0) {
continue ;
}
if (i % 3 == 0 || i % 7 == 0) {
continue ;
}
int sum = i % 9;
if (sum == 0 || sum == 1 || sum == 4 || sum == 7
|| sum == 9) {
continue ;
}
if (isPrime(i) && isPrime(sum)) {
count++;
}
}
return count;
}
public static void Main()
{
int L = 1, R = 10;
Console.Write(countSingleDigitPrimes(L, R));
}
}
|
Javascript
function isPrime(n)
{
if (n <= 1) {
return false ;
}
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
function countSingleDigitPrimes(l, r) {
let count = 0;
for (let i = 2; i <= 7 && i <= r; i++) {
if (l <= i && isPrime(i)) {
count++;
}
}
for (let i = Math.max(l, 8); i <= r; i++) {
if (i % 2 == 0 || i % 5 == 0) {
continue ;
}
if (i % 3 == 0 || i % 7 == 0) {
continue ;
}
let sum = i % 9;
if (sum == 0 || sum == 1 || sum == 4 || sum == 7 || sum == 9) {
continue ;
}
if (isPrime(i) && isPrime(sum)) {
count++;
}
}
return count;
}
let L = 1,
R = 10;
console.log(countSingleDigitPrimes(L, R));
|
Output:
4
Time Complexity: O((R – L + 1) * sqrt(R))
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...