Given a range, the task is to find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum.
Input: l = 2, r = 10 Output: 5 2, 3, 4, 5 and 7 are such numbers Input: l = 15, r = 22 Output: 3 17, 19 and 22 are such numbers As, 17 and 19 are already prime. Prime Factors of 22 = 2 * 11 i.e For 22, Sum of digits is 2+2 = 4 For 2 * 11, Sum of digits is 2 + 1 + 1 = 4
Approach: An efficient solution is to modify Sieve of Eratosthenes such that for each non-prime number it stores smallest prime factor(prefactor).
- Preprocess to find the smallest prime factorfor all the numbers between 2 and MAXN. This can be done by breaking up the number into its prime factors in constant time because for each number if it is a prime, it has no prefactor.
- Otherwise, we can break it up to into a prime factor and the other part of the number which may or may not be prime.
- And repeat this process of extracting factors till it becomes a prime.
- Then check if the digits of that number is equal to the digits of prime factors by adding th digits of smallest prime factor i.e.
Digits_Sum of SPF[n] + Digits_Sum of (n / SPF[n])
- Now make prefix sum array that counts how many valid numbers are there up to a number N. For each query, print:
ans[R] – ans[L-1]
Below is the implementation of above approach:
# Python 3 program to Find the count of
# the numbers in the given range such
# that the sum of its digit is equal to
# the sum of all its prime factors digits sum.
# maximum size of number
MAXN = 100005
# array to store smallest prime
# factor of number
spf =  * MAXN
# array to store sum of digits of a number
sum_digits =  * MAXN
# boolean array to check given number
# is countable for required answer or not.
isValid =  * MAXN
# prefix array to store answer
ans = *MAXN
# Calculating SPF (Smallest Prime Factor)
# for every number till MAXN.
# marking smallest prime factor
# for every number to be itself.
for i in range(1, MAXN):
spf[i] = i
# separately marking spf for
# every even number as 2
for i in range(4, MAXN, 2):
spf[i] = 2
i = 3
while i * i <= MAXN: # checking if i is prime if (spf[i] == i): # marking SPF for all numbers # divisible by i for j in range(i * i, MAXN, i): # marking spf[j] if it is not # previously marked if (spf[j] == j): spf[j] = i i += 2 # Function to find sum of digits # in a number def Digit_Sum(copy): d = 0 while (copy) : d += copy % 10 copy //= 10 return d # find sum of digits of all # numbers up to MAXN def Sum_Of_All_Digits(): for n in range(2, MAXN) : # add sum of digits of least # prime factor and n/spf[n] sum_digits[n] = (sum_digits[n // spf[n]] + Digit_Sum(spf[n])) # if it is valid make isValid true if (Digit_Sum(n) == sum_digits[n]): isValid[n] = True # prefix sum to compute answer for n in range(2, MAXN) : if (isValid[n]): ans[n] = 1 ans[n] += ans[n - 1] # Driver code if __name__ == "__main__": Smallest_prime_factor() Sum_Of_All_Digits() # print answer for required range l = 2 r = 3 print("Valid numbers in the range", l, r, "are", ans[r] - ans[l - 1]) # print answer for required range l = 2 r = 10 print("Valid numbers in the range", l, r, "are", ans[r] - ans[l - 1]) # This code is contributed by ita_c [tabby title="C#"]
Valid numbers in the range 2 3 are 2 Valid numbers in the range 2 10 are 5
- Print prime numbers with prime sum of digits in an array
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Find maximum product of digits among numbers less than or equal to N
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Sum of largest prime factor of each number less than equal to n
- Exactly n distinct prime factor numbers from a to b
- Least prime factor of numbers till n
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Largest number with prime digits
- Recursive sum of digits of a number is prime or not
- Sum of digits equal to a given number in PL/SQL
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.