# Count all prime numbers in a given range whose sum of digits is also prime

Given two integers L and R, the task is to find the count of total numbers of prime numbers in the range [L, R] whose sum of the digits is also a prime number.

Examples:

Input: L = 1, R = 10
Output: 4
Explanation:
Prime numbers in the range L = 1 to R = 10 are {2, 3, 5, 7}.
Their sum of digits is {2, 3, 5, 7}.
Since all the numbers are prime, hence the answer to the query is 4.

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}.1
Their sum of digits is {5, 7, 2, 4, 8, 10}.
Only {5, 7, 2} are prime, hence the answer to the query is 3.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive 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 sum of its digits and again check whether the sum is prime or not. If the sum is prime, then increment the counter for the current element in the range [L, R].

Time Complexity: O((R – L)*log(log P)) where P is the prime number in the range [L, R].

Efficient Approach:

1. Store all the prime numbers ranging from 1 to 106 in an array using Sieve of Eratosthenes.
2. Create another array that will store whether the sum of the digits of all the numbers ranging from 1 to 106 which are prime.
3. Now, compute a prefix array to store counts till every value before limit.
4. Once we have prefix array, the value of prefix[R] – prefix[L-1] gives the count of element in the given range which are prime and whose sum are also prime.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `int` `maxN = 1000000; ` ` `  `// Create an array for storing primes ` `int` `arr; ` ` `  `// Create a prefix array that will ` `// contain whether sum is prime or not ` `int` `prefix; ` ` `  `// Function to find primes in the range ` `// and check whether the sum of digits ` `// of a prime number is prime or not ` `void` `findPrimes() ` `{ ` `    ``// Initialise Prime array arr[] ` `    ``for` `(``int` `i = 1; i <= maxN; i++) ` `        ``arr[i] = 1; ` ` `  `    ``// Since 0 and 1 are not prime ` `    ``// numbers we mark them as '0' ` `    ``arr = 0, arr = 0; ` ` `  `    ``// Using Sieve Of Eratosthenes ` `    ``for` `(``int` `i = 2; i * i <= maxN; i++) { ` ` `  `        ``// if the number is prime ` `        ``if` `(arr[i] == 1) { ` ` `  `            ``// Mark all the multiples ` `            ``// of i starting from sqaure ` `            ``// of i with '0' ie. composite ` `            ``for` `(``int` `j = i * i; ` `                 ``j <= maxN; j += i) { ` ` `  `                ``//'0' represents not prime ` `                ``arr[j] = 0; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Initialise a sum variable as 0 ` `    ``int` `sum = 0; ` `    ``prefix = 0; ` ` `  `    ``for` `(``int` `i = 1; i <= maxN; i++) { ` ` `  `        ``// Check if the number is prime ` `        ``if` `(arr[i] == 1) { ` ` `  `            ``// A temporary variable to ` `            ``// store the number ` `            ``int` `temp = i; ` `            ``sum = 0; ` ` `  `            ``// Loop to calculate the ` `            ``// sum of digits ` `            ``while` `(temp > 0) { ` `                ``int` `x = temp % 10; ` `                ``sum += x; ` `                ``temp = temp / 10; ` ` `  `                ``// Check if the sum of prime ` `                ``// number is prime ` `                ``if` `(arr[sum] == 1) { ` ` `  `                    ``// if prime mark 1 ` `                    ``prefix[i] = 1; ` `                ``} ` ` `  `                ``else` `{ ` ` `  `                    ``// If not prime mark 0 ` `                    ``prefix[i] = 0; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// computing prefix array ` `    ``for` `(``int` `i = 1; i <= maxN; i++) { ` `        ``prefix[i] ` `            ``+= prefix[i - 1]; ` `    ``} ` `} ` ` `  `// Function to count the prime numbers ` `// in the range [L, R] ` `void` `countNumbersInRange(``int` `l, ``int` `r) ` `{ ` `    ``// Function Call to find primes ` `    ``findPrimes(); ` `    ``int` `result = prefix[r] ` `                 ``- prefix[l - 1]; ` ` `  `    ``// Print the result ` `    ``cout << result << endl; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Input range ` `    ``int` `l, r; ` `    ``l = 5, r = 20; ` ` `  `    ``// Function Call ` `    ``countNumbersInRange(l, r); ` `    ``return` `0; ` `} `

## Java

 `// Java program for the above approach ` `class` `GFG{ ` ` `  `static` `int` `maxN = ``1000000``; ` ` `  `// Create an array for storing primes ` `static` `int` `[]arr = ``new` `int``[``1000001``]; ` ` `  `// Create a prefix array that will ` `// contain whether sum is prime or not ` `static` `int` `[]prefix = ``new` `int``[``1000001``]; ` ` `  `// Function to find primes in the range ` `// and check whether the sum of digits ` `// of a prime number is prime or not ` `static` `void` `findPrimes() ` `{ ` `    ``// Initialise Prime array arr[] ` `    ``for` `(``int` `i = ``1``; i <= maxN; i++) ` `        ``arr[i] = ``1``; ` ` `  `    ``// Since 0 and 1 are not prime ` `    ``// numbers we mark them as '0' ` `    ``arr[``0``] = ``0``; ` `    ``arr[``1``] = ``0``; ` ` `  `    ``// Using Sieve Of Eratosthenes ` `    ``for` `(``int` `i = ``2``; i * i <= maxN; i++) ` `    ``{ ` ` `  `        ``// if the number is prime ` `        ``if` `(arr[i] == ``1``) ` `        ``{ ` ` `  `            ``// Mark all the multiples ` `            ``// of i starting from sqaure ` `            ``// of i with '0' ie. composite ` `            ``for` `(``int` `j = i * i; ` `                     ``j <= maxN; j += i)  ` `            ``{ ` ` `  `                ``//'0' represents not prime ` `                ``arr[j] = ``0``; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Initialise a sum variable as 0 ` `    ``int` `sum = ``0``; ` `    ``prefix[``0``] = ``0``; ` ` `  `    ``for` `(``int` `i = ``1``; i <= maxN; i++)  ` `    ``{ ` ` `  `        ``// Check if the number is prime ` `        ``if` `(arr[i] == ``1``) ` `        ``{ ` ` `  `            ``// A temporary variable to ` `            ``// store the number ` `            ``int` `temp = i; ` `            ``sum = ``0``; ` ` `  `            ``// Loop to calculate the ` `            ``// sum of digits ` `            ``while` `(temp > ``0``) ` `            ``{ ` `                ``int` `x = temp % ``10``; ` `                ``sum += x; ` `                ``temp = temp / ``10``; ` ` `  `                ``// Check if the sum of prime ` `                ``// number is prime ` `                ``if` `(arr[sum] == ``1``)  ` `                ``{ ` ` `  `                    ``// if prime mark 1 ` `                    ``prefix[i] = ``1``; ` `                ``} ` ` `  `                ``else`  `                ``{ ` ` `  `                    ``// If not prime mark 0 ` `                    ``prefix[i] = ``0``; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// computing prefix array ` `    ``for` `(``int` `i = ``1``; i <= maxN; i++) ` `    ``{ ` `        ``prefix[i] += prefix[i - ``1``]; ` `    ``} ` `} ` ` `  `// Function to count the prime numbers ` `// in the range [L, R] ` `static` `void` `countNumbersInRange(``int` `l, ``int` `r) ` `{ ` `    ``// Function Call to find primes ` `    ``findPrimes(); ` `    ``int` `result = prefix[r] - prefix[l - ``1``]; ` ` `  `    ``// Print the result ` `    ``System.out.print(result + ``"\n"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``// Input range ` `    ``int` `l, r; ` `    ``l = ``5``; ` `    ``r = ``20``; ` ` `  `    ``// Function Call ` `    ``countNumbersInRange(l, r); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

## C#

 `// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` `  `static` `int` `maxN = 1000000; ` ` `  `// Create an array for storing primes ` `static` `int` `[]arr = ``new` `int``; ` ` `  `// Create a prefix array that will ` `// contain whether sum is prime or not ` `static` `int` `[]prefix = ``new` `int``; ` ` `  `// Function to find primes in the range ` `// and check whether the sum of digits ` `// of a prime number is prime or not ` `static` `void` `findPrimes() ` `{ ` `    ``// Initialise Prime array arr[] ` `    ``for` `(``int` `i = 1; i <= maxN; i++) ` `        ``arr[i] = 1; ` ` `  `    ``// Since 0 and 1 are not prime ` `    ``// numbers we mark them as '0' ` `    ``arr = 0; ` `    ``arr = 0; ` ` `  `    ``// Using Sieve Of Eratosthenes ` `    ``for` `(``int` `i = 2; i * i <= maxN; i++) ` `    ``{ ` ` `  `        ``// if the number is prime ` `        ``if` `(arr[i] == 1) ` `        ``{ ` ` `  `            ``// Mark all the multiples ` `            ``// of i starting from sqaure ` `            ``// of i with '0' ie. composite ` `            ``for` `(``int` `j = i * i; ` `                     ``j <= maxN; j += i)  ` `            ``{ ` ` `  `                ``//'0' represents not prime ` `                ``arr[j] = 0; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Initialise a sum variable as 0 ` `    ``int` `sum = 0; ` `    ``prefix = 0; ` ` `  `    ``for` `(``int` `i = 1; i <= maxN; i++)  ` `    ``{ ` ` `  `        ``// Check if the number is prime ` `        ``if` `(arr[i] == 1) ` `        ``{ ` ` `  `            ``// A temporary variable to ` `            ``// store the number ` `            ``int` `temp = i; ` `            ``sum = 0; ` ` `  `            ``// Loop to calculate the ` `            ``// sum of digits ` `            ``while` `(temp > 0) ` `            ``{ ` `                ``int` `x = temp % 10; ` `                ``sum += x; ` `                ``temp = temp / 10; ` ` `  `                ``// Check if the sum of prime ` `                ``// number is prime ` `                ``if` `(arr[sum] == 1)  ` `                ``{ ` ` `  `                    ``// if prime mark 1 ` `                    ``prefix[i] = 1; ` `                ``} ` ` `  `                ``else` `                ``{ ` ` `  `                    ``// If not prime mark 0 ` `                    ``prefix[i] = 0; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// computing prefix array ` `    ``for` `(``int` `i = 1; i <= maxN; i++) ` `    ``{ ` `        ``prefix[i] += prefix[i - 1]; ` `    ``} ` `} ` ` `  `// Function to count the prime numbers ` `// in the range [L, R] ` `static` `void` `countNumbersInRange(``int` `l, ``int` `r) ` `{ ` `    ``// Function Call to find primes ` `    ``findPrimes(); ` `    ``int` `result = prefix[r] - prefix[l - 1]; ` ` `  `    ``// Print the result ` `    ``Console.Write(result + ``"\n"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``// Input range ` `    ``int` `l, r; ` `    ``l = 5; ` `    ``r = 20; ` ` `  `    ``// Function Call ` `    ``countNumbersInRange(l, r); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

Output:

```3
```

Time Complexity: O(N)
Auxiliary Space: O(N) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : sapnasingh4991, Code_Mech