Count prime numbers in range [L, R] whose single digit sum is also prime - GeeksforGeeks

19 Jan, 2022
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: 3Explanation: 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: 4Explanation: 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
Java
Python3
C#
Javascript
// C++ program for above approach#include <bits/stdc++.h>using namespace std; // Function to check whether number// is prime or notbool isPrime(int n){    // Corner case    if (n <= 1)        return false;     // Check from 2 to square root of n    for (int i = 2; i <= sqrt(n); i++)        if (n % i == 0)            return false;     return true;} // Function to find single digit sumint SingleDigitSum(int& n){    if (n <= 9)        return n;    return (n % 9 == 0) ? 9 : n % 9;} // Function to find single digit primesint countSingleDigitPrimes(int l, int r){    int count = 0, i;     for (i = l; i <= r; i++) {        if (isPrime(i)            && isPrime(SingleDigitSum(i))) {            count++;        }    }    return count;} // Driver Codeint main(){    // Input range    int L = 1, R = 10;     // Function Call    cout << countSingleDigitPrimes(L, R);     return 0;}
// Java program to implement// the above approachclass GFG{   // Function to check whether number  // is prime or not  static boolean isPrime(int n)  {     // Corner case    if (n <= 1)      return false;     // Check from 2 to square root of n    for (int i = 2; i <= Math.sqrt(n); i++)      if (n % i == 0)        return false;     return true;  }   // Function to find single digit sum  static int SingleDigitSum(int n)  {    if (n <= 9)      return n;    return (n % 9 == 0) ? 9 : n % 9;  }   // Function to find single digit primes  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;  }   // Driver Code  public static void main(String args[])  {     // Input range    int L = 1, R = 10;     // Function Call    System.out.println(countSingleDigitPrimes(L, R));   }} // This code is contributed by gfgking
# Python program for above approachimport math # Function to check whether number# is prime or notdef isPrime(n):       # Corner case    if n <= 1:        return False     # Check from 2 to square root of n    for i in range(2, math.floor(math.sqrt(n)) + 1):        if n % i == 0:            return False     return True # Function to find single digit sumdef SingleDigitSum(n):    if n <= 9:        return n    return 9 if (n % 9 == 0) else n % 9 # Function to find single digit primesdef 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 # Driver Code # Input rangeL = 1R = 10 # Function Callprint(countSingleDigitPrimes(L, R)) # This code is contributed by gfgking
// C# program to implement// the above approachusing System;class GFG{   // Function to check whether number  // is prime or not  static bool isPrime(int n)  {     // Corner case    if (n <= 1)      return false;     // Check from 2 to square root of n    for (int i = 2; i <= Math.Sqrt(n); i++)      if (n % i == 0)        return false;     return true;  }   // Function to find single digit sum  static int SingleDigitSum(int n)  {    if (n <= 9)      return n;    return (n % 9 == 0) ? 9 : n % 9;  }   // Function to find single digit primes  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;  }   // Driver Code  public static void Main()  {     // Input range    int L = 1, R = 10;     // Function Call    Console.Write(countSingleDigitPrimes(L, R));   }} // This code is contributed by Samim Hossain Mondal.
<script>    // JavaScript program for above approach     // Function to check whether number    // is prime or not    const isPrime = (n) => {        // Corner case        if (n <= 1)            return false;         // Check from 2 to square root of n        for (let i = 2; i <= Math.sqrt(n); i++)            if (n % i == 0)                return false;         return true;    }     // Function to find single digit sum    const SingleDigitSum = (n) => {        if (n <= 9)            return n;        return (n % 9 == 0) ? 9 : n % 9;    }     // Function to find single digit primes    const countSingleDigitPrimes = (l, r) => {        let count = 0, i;         for (i = l; i <= r; i++) {            if (isPrime(i)                && isPrime(SingleDigitSum(i))) {                count++;            }        }        return count;    }     // Driver Code     // Input range    let L = 1, R = 10;     // Function Call    document.write(countSingleDigitPrimes(L, R)); // This code is contributed by rakeshsahni </script>
4
 Time Complexity: O((R – L)*N^(1/2)) where N is the prime number in the range [L, R]. Auxiliary Space: O(1)
 
rakeshsahni
gfgking
samim2000
germanshephered48
Algo-Geek 2021
array-range-queries
Prime Number
Algo Geek
Mathematical
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to find simple moving average | Set-2
Divide given number into two even parts
Check if the given string is valid English word or not
Count of Palindrome Strings in given Array of strings
Sorting given character Array using Linked List
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Coin Change | DP-7