Given two integers L and R, the task is to count all the numbers in the range [L, R] whose sum of digits is a prime number.
Examples:
Input: L = 1, R = 10
Output: 3
Explanation:
Numbers in the range L(= 1) to R(= 10), whose sum of digits is a prime number are {3, 5, 7}. Therefore, the required output is 3.Input: L = 11, R = 999
Output: 336
Naive Approach: The simplest approach to solve this problem is to traverse all the numbers in the range [L, R] and for each number, check if the sum of digits of the number is a prime number or not. If found to be true, increment the count and finally, print the count as the required answer.
Time Complexity: O((R – L + 1) * sqrt(R))
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved using Digit DP. The idea is to count the numbers in the range [1, R] whose sum of digits is a prime number and subtract the count of the numbers in the range [1, L – 1] whose sum of digits is a prime number. Following are the recurrence relations:
[Tex]= \sum^{9}_{i=0} cnt1XPrime(sum + i, len + 1, tight \& (i==end)) [/Tex]
cnt1XPrime(sum, len, tight): Stores the count of numbers in the range [1, X] with the following constraints:
sum= Stores sum of digits of a number in the range [1, X].
len = count of digits in X.
tight = Boolean value to check if the current digits range is restricted or not.
Follow the steps below to solve the problem:
- Initialize a 3D array dp[sum][len][tight] to compute and store the values of all subproblems of the above recurrence relation.
- Finally, return the value of dp[sum][len][tight].
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to find all prime numbers// in the range [1, 100000] using// Sieve of Eratosthenes techniquevector<bool> seieve(){ // isPrime[i] stores if i // is a prime number or not. vector<bool> isPrime(100001, true); // 0 is not a prime number isPrime[0] = false; // 1 is not prime number isPrime[1] = false; // Traverse the range to check if // i is a prime number or not for (int i = 2; i * i < 100001; i++) { // If i is a prime number if (isPrime[i]) { for (int j = i * i; j < 100001; j += i) { // Mark its multiples non-prime isPrime[j] = false; } } } return isPrime;}// Function to count all numbers in// the range[1, X] whose sum of digits// is a prime numberint cnt1XPrime(int sum, int len, bool tight, string X, vector<bool>& isPrime, int dp[1000][100][2]){ // If count of digits in current number // is equal to the count of digits in X if (len == X.length()) { // If sum is a prime number return isPrime[sum]; } // If already computed subproblem // occurred if (dp[sum][len][tight] != -1) { return dp[sum][len][tight]; } // Stores maximum possible value // at current digit of the number int end = tight ? (X[len] - '0') : 9; // Stores count of numbers by placing // all possible values at current index int res = 0; // Place all possible values at // current position for (int i = 0; i <= end; i++) { // Update res res += cnt1XPrime(sum + i, len + 1, (tight & (i == end)), X, isPrime, dp); } return res;}// Function to count the numbers in// the range[L, R]int cntLRprime(int L, int R){ // Stores the value of (L - 1) // in the form of string string LStr = to_string(L - 1); // Stores the value of (R) // in the form of string string RStr = to_string(R); // Stores values of overlapping // subproblems int dp[1000][100][2]; // Initalize dp[][][] array memset(dp, -1, sizeof(dp)); // isPrime[i] stores if i // is a prime number or not vector<bool> isPrime = seieve(); // Stores count of numbers in range // [1, LStr] with the given conditions int cntL = cnt1XPrime(0, 0, 1, LStr, isPrime, dp); // Initalize dp[][][] array. memset(dp, -1, sizeof(dp)); // Stores count of numbers in range // [1, RStr] with the given conditions int cntR = cnt1XPrime(0, 0, 1, RStr, isPrime, dp); // Return numbers in the range [L, R] // whose sum of digits is a prime number return (cntR - cntL);}// Driver Codeint main(){ int L = 11, R = 999; cout << cntLRprime(L, R); return 0;} |
Python3
# Python3 program to implement# the above approachisPrime = [True] * 100001dp = [[[-1 for i in range(2)] for i in range(100)] for i in range(1000)] # Function to find all prime numbers# in the range [1, 100000] using# Sieve of Eratosthenes techniquedef seieve(): # 0 is not a prime number isPrime[0] = False # 1 is not prime number isPrime[1] = False # Traverse the range to check if # i is a prime number or not for i in range(2, 100001): if i * i > 100001: break # If i is a prime number if (isPrime[i]): for j in range(i * i, 100001, i): # Mark its multiples non-prime isPrime[j] = False# Function to count all numbers in# the range[1, X] whose sum of digits# is a prime numberdef cnt1XPrime(sum, lenn, tight, X): # If count of digits in current number # is equal to the count of digits in X if (lenn == len(X)): # If sum is a prime number return isPrime[sum] # If already computed subproblem # occurred if (dp[sum][lenn][tight] != -1): return dp[sum][lenn][tight] # Stores maximum possible value # at current digit of the number end = 9 if tight: end = ord(X[lenn]) - ord('0') # Stores count of numbers by placing # all possible values at current index res = 0 # Place all possible values at # current position for i in range(end + 1): # Update res res += cnt1XPrime(sum + i, lenn + 1, (tight & (i == end)), X) return res# Function to count the numbers in# the range[L, R]def cntLRprime(L, R): # Stores the value of (L - 1) # in the form of string LStr = str(L - 1) # Stores the value of (R) # in the form of string RStr = str(R) # isPrime[i] stores if i # is a prime number or not seieve() # Stores count of numbers in range # [1, LStr] with the given conditions cntL = cnt1XPrime(0, 0, 1, LStr) # Initalize dp[][][] array. for i in range(1000): for j in range(100): for z in range(2): dp[i][j][z] = -1 # Stores count of numbers in range # [1, RStr] with the given conditions cntR = cnt1XPrime(0, 0, 1, RStr) # Return numbers in the range [L, R] # whose sum of digits is a prime number return (cntR - cntL)# Driver codeif __name__ == '__main__': L = 11 R = 999 print(cntLRprime(L, R))# This code is contributed by mohit kumar 29 |
336
Time Complexity: O(sum * M * 10)
Auxiliary Space: O(sum * M), where sum denotes the maximum sum of digits of a number in the range [L, R] and M denotes the number of digits in R.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count all prime numbers in a given range whose sum of digits is also prime
- Count numbers in a given range whose count of prime factors is a Prime Number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count of numbers from range [L, R] whose sum of digits is Y
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Print prime numbers with prime sum of digits in an array
- Sum of prime numbers without odd prime digits
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count of n digit numbers whose sum of digits equals to given sum
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
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 : mohit kumar 29

