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.
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].
Space Complexity: O(N)
Efficient Approach:
- Store all the prime numbers ranging from 1 to 106 in an array using Sieve of Eratosthenes.
- Create another array that will store whether the sum of the digits of all the numbers ranging from 1 to 106 which are prime.
- Now, compute a prefix array to store counts till every value before the limit.
- Once we have a prefix array, the value of prefix[R] – prefix[L-1] gives the count of elements in the given range that are prime and whose sum is also prime.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxN = 1000000;
int arr[1000001];
int prefix[1000001];
void findPrimes()
{
for ( int i = 1; i <= maxN; i++)
arr[i] = 1;
arr[0] = 0, arr[1] = 0;
for ( int i = 2; i * i <= maxN; i++) {
if (arr[i] == 1) {
for ( int j = i * i;
j <= maxN; j += i) {
arr[j] = 0;
}
}
}
int sum = 0;
prefix[0] = 0;
for ( int i = 1; i <= maxN; i++) {
if (arr[i] == 1) {
int temp = i;
sum = 0;
while (temp > 0) {
int x = temp % 10;
sum += x;
temp = temp / 10;
if (arr[sum] == 1) {
prefix[i] = 1;
}
else {
prefix[i] = 0;
}
}
}
}
for ( int i = 1; i <= maxN; i++) {
prefix[i]
+= prefix[i - 1];
}
}
void countNumbersInRange( int l, int r)
{
findPrimes();
int result = prefix[r]
- prefix[l - 1];
cout << result << endl;
}
int main()
{
int l, r;
l = 5, r = 20;
countNumbersInRange(l, r);
return 0;
}
|
Java
class GFG{
static int maxN = 1000000 ;
static int []arr = new int [ 1000001 ];
static int []prefix = new int [ 1000001 ];
static void findPrimes()
{
for ( int i = 1 ; i <= maxN; i++)
arr[i] = 1 ;
arr[ 0 ] = 0 ;
arr[ 1 ] = 0 ;
for ( int i = 2 ; i * i <= maxN; i++)
{
if (arr[i] == 1 )
{
for ( int j = i * i;
j <= maxN; j += i)
{
arr[j] = 0 ;
}
}
}
int sum = 0 ;
prefix[ 0 ] = 0 ;
for ( int i = 1 ; i <= maxN; i++)
{
if (arr[i] == 1 )
{
int temp = i;
sum = 0 ;
while (temp > 0 )
{
int x = temp % 10 ;
sum += x;
temp = temp / 10 ;
if (arr[sum] == 1 )
{
prefix[i] = 1 ;
}
else
{
prefix[i] = 0 ;
}
}
}
}
for ( int i = 1 ; i <= maxN; i++)
{
prefix[i] += prefix[i - 1 ];
}
}
static void countNumbersInRange( int l, int r)
{
findPrimes();
int result = prefix[r] - prefix[l - 1 ];
System.out.print(result + "\n" );
}
public static void main(String[] args)
{
int l, r;
l = 5 ;
r = 20 ;
countNumbersInRange(l, r);
}
}
|
Python3
maxN = 1000000
arr = [ 0 ] * ( 1000001 )
prefix = [ 0 ] * ( 1000001 )
def findPrimes():
for i in range ( 1 , maxN + 1 ):
arr[i] = 1
arr[ 0 ] = 0
arr[ 1 ] = 0
i = 2
while i * i < = maxN:
if (arr[i] = = 1 ):
for j in range (i * i, maxN, i):
arr[j] = 0
i + = 1
sum = 0
prefix[ 0 ] = 0
for i in range ( 1 , maxN + 1 ):
if (arr[i] = = 1 ):
temp = i
sum = 0
while (temp > 0 ):
x = temp % 10
sum + = x
temp = temp / / 10
if (arr[ sum ] = = 1 ):
prefix[i] = 1
else :
prefix[i] = 0
for i in range ( 1 , maxN + 1 ):
prefix[i] + = prefix[i - 1 ]
def countNumbersInRange(l, r):
findPrimes()
result = (prefix[r] - prefix[l - 1 ])
print (result)
if __name__ = = "__main__" :
l = 5
r = 20
countNumbersInRange(l, r)
|
C#
using System;
class GFG{
static int maxN = 1000000;
static int []arr = new int [1000001];
static int []prefix = new int [1000001];
static void findPrimes()
{
for ( int i = 1; i <= maxN; i++)
arr[i] = 1;
arr[0] = 0;
arr[1] = 0;
for ( int i = 2; i * i <= maxN; i++)
{
if (arr[i] == 1)
{
for ( int j = i * i;
j <= maxN; j += i)
{
arr[j] = 0;
}
}
}
int sum = 0;
prefix[0] = 0;
for ( int i = 1; i <= maxN; i++)
{
if (arr[i] == 1)
{
int temp = i;
sum = 0;
while (temp > 0)
{
int x = temp % 10;
sum += x;
temp = temp / 10;
if (arr[sum] == 1)
{
prefix[i] = 1;
}
else
{
prefix[i] = 0;
}
}
}
}
for ( int i = 1; i <= maxN; i++)
{
prefix[i] += prefix[i - 1];
}
}
static void countNumbersInRange( int l, int r)
{
findPrimes();
int result = prefix[r] - prefix[l - 1];
Console.Write(result + "\n" );
}
public static void Main()
{
int l, r;
l = 5;
r = 20;
countNumbersInRange(l, r);
}
}
|
Javascript
<script>
let maxN = 1000000;
let arr = Array.from({length: 1000001}, (_, i) => 0);
let prefix = Array.from({length: 1000001}, (_, i) => 0);
function findPrimes()
{
for (let i = 1; i <= maxN; i++)
arr[i] = 1;
arr[0] = 0;
arr[1] = 0;
for (let i = 2; i * i <= maxN; i++)
{
if (arr[i] == 1)
{
for (let j = i * i;
j <= maxN; j += i)
{
arr[j] = 0;
}
}
}
let sum = 0;
prefix[0] = 0;
for (let i = 1; i <= maxN; i++)
{
if (arr[i] == 1)
{
let temp = i;
sum = 0;
while (temp > 0)
{
let x = temp % 10;
sum += x;
temp = Math.floor(temp / 10);
if (arr[sum] == 1)
{
prefix[i] = 1;
}
else
{
prefix[i] = 0;
}
}
}
}
for (let i = 1; i <= maxN; i++)
{
prefix[i] += prefix[i - 1];
}
}
function countNumbersInRange(l, r)
{
findPrimes();
let result = prefix[r] - prefix[l - 1];
document.write(result + "\n" );
}
let l, r;
l = 5;
r = 20;
countNumbersInRange(l, r);
</script>
|
Time Complexity: O(N*(log(log)N))
Auxiliary Space: O(N)
Approach: Brute force approach
Steps:
- Define a function is_prime(num) to check if a number is prime or not.
- Define a function sum_of_digits(num) to find the sum of digits of a number.
- Iterate through the range [L, R], and for each number in the range, check if it is prime and if the sum of its digits is prime.
- If both conditions are satisfied, increment the count.
- Return the count as the result.
C++
#include <iostream>
#include <cmath>
using namespace std;
bool is_prime( int num) {
if (num <= 1) {
return false ;
}
for ( int i = 2; i <= sqrt (num); i++) {
if (num % i == 0) {
return false ;
}
}
return true ;
}
int sum_of_digits( int num) {
int s = 0;
while (num > 0) {
s += num % 10;
num /= 10;
}
return s;
}
int count_primes_with_prime_sum_of_digits( int L, int R) {
int count = 0;
for ( int num = L; num <= R; num++) {
if (is_prime(num) && is_prime(sum_of_digits(num))) {
count++;
}
}
return count;
}
int main() {
int L = 1;
int R = 10;
int result = count_primes_with_prime_sum_of_digits(L, R);
cout << result << endl;
return 0;
}
|
Java
public class Main {
public static boolean isPrime( int num) {
if (num <= 1 ) {
return false ;
}
for ( int i = 2 ; i <= Math.sqrt(num); i++) {
if (num % i == 0 ) {
return false ;
}
}
return true ;
}
public static int sumOfDigits( int num) {
int s = 0 ;
while (num > 0 ) {
s += num % 10 ;
num /= 10 ;
}
return s;
}
public static int countPrimesWithPrimeSumOfDigits( int L, int R) {
int count = 0 ;
for ( int num = L; num <= R; num++) {
if (isPrime(num) && isPrime(sumOfDigits(num))) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int L = 1 ;
int R = 10 ;
int result = countPrimesWithPrimeSumOfDigits(L, R);
System.out.println(result);
}
}
|
Python3
def is_prime(num):
if num < = 1 :
return False
for i in range ( 2 , int (num * * 0.5 ) + 1 ):
if num % i = = 0 :
return False
return True
def sum_of_digits(num):
s = 0
while num > 0 :
s + = num % 10
num / / = 10
return s
def count_primes_with_prime_sum_of_digits(L, R):
count = 0
for num in range (L, R + 1 ):
if is_prime(num) and is_prime(sum_of_digits(num)):
count + = 1
return count
L = 1
R = 10
result = count_primes_with_prime_sum_of_digits(L, R)
print (result)
|
C#
using System;
class Program
{
static bool IsPrime( int num)
{
if (num <= 1)
{
return false ;
}
for ( int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
{
return false ;
}
}
return true ;
}
static int SumOfDigits( int num)
{
int s = 0;
while (num > 0)
{
s += num % 10;
num /= 10;
}
return s;
}
static int CountPrimesWithPrimeSumOfDigits( int L, int R)
{
int count = 0;
for ( int num = L; num <= R; num++)
{
if (IsPrime(num) && IsPrime(SumOfDigits(num)))
{
count++;
}
}
return count;
}
static void Main()
{
int L = 1;
int R = 10;
int result = CountPrimesWithPrimeSumOfDigits(L, R);
Console.WriteLine(result);
}
}
|
Javascript
function isPrime(num) {
if (num <= 1) {
return false ;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false ;
}
}
return true ;
}
function sumOfDigits(num) {
let sum = 0;
while (num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
}
return sum;
}
function countPrimesWithPrimeSumOfDigits(L, R) {
let count = 0;
for (let num = L; num <= R; num++) {
if (isPrime(num) && isPrime(sumOfDigits(num))) {
count++;
}
}
return count;
}
let L = 1;
let R = 10;
let result = countPrimesWithPrimeSumOfDigits(L, R);
console.log(result);
|
Time complexity: O((R-L)*sqrt(R))
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Sep, 2023
Like Article
Save Article