Given a range L and R count all numbers between L to R such that sum of digit of each number and sum of square of digit of each number is Prime.
Note: 10 <= [L, R] <= 108
Input: L = 10, R = 20
Such types of numbers are: 11 12 14 16
Input: L = 100, R = 130
Such types of numbers are : 101 102 104 106 110 111 113 119 120
Just get the sum of digit of each number and sum of the square of digit of each number and to check whether they both are prime or not.
In this approach, there is an observation that can do the optimization:
- Now if look closely into range the number is 108 ie., and largest number less than this will be 99999999 and maximum number can be formed is 8 * ( 9 * 9 ) = 648 (as the sum of digit square is 92 + 92 + … 8times) so we need only primes upto 648 only which can be done using Sieve of Eratosthenes.
- Now iterate for each number in the range and check whether it satisfies above condition or not.
Below is the implementation of the above approach:
If there are multiple queries asked to find out numbers between range from L and R there will be 2 approaches:
- Store all number which satisfies above condition in another array and use binary search to find out how many elements in array such that it less than R , say cnt1 , and how many elements in array such that it less than L , say cnt2 . Return cnt1 – cnt2
Time Complexity: O(log(N)) per query.
- We can use prefix array or DP approach such that it already stores how many no. are good of above type from index 0 to i, and return total count by giving DP[R] – DP[L-1]
Time Complexity: O(1) per query.
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.
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count all prime numbers in a given range whose sum of digits is also prime
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Find the highest occurring digit in prime numbers in a range
- Sum of all prime divisors of all the numbers in range L-R
- Count numbers in a given range whose count of prime factors is a Prime Number
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of Numbers in Range where first digit is equal to last digit of the number
- C/C++ Program to find Prime Numbers between given range
- Sum of all the prime numbers in a given range
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Queries for the difference between the count of composite and prime numbers in a given range
- Count pairs from a given range whose sum is a Prime Number in that range
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.