Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
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
Examples:
Input: L = 10, R = 20
Output: 4
Such types of numbers are: 11 12 14 16Input: L = 100, R = 130
Output: 9Such types of numbers are : 101 102 104 106 110 111 113 119 120
Naive Approach:
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.
Efficient Approach:
-
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Sieve of prime numbers void primesieve(vector< bool >& prime) { // Sieve to store whether a // number is prime or not in // O(nlog(log(n))) prime[1] = false ; for ( int p = 2; p * p <= 650; p++) { if (prime[p] == true ) { for ( int i = p * 2; i <= 650; i += p) prime[i] = false ; } } } // Function to return sum of digit // and sum of square of digit pair< int , int > sum_sqsum( int n) { int sum = 0; int sqsum = 0; int x; // Until number is not // zero while (n) { x = n % 10; sum += x; sqsum += x * x; n /= 10; } return (make_pair(sum, sqsum)); } // Function to return the count // of number form L to R // whose sum of digits and // sum of square of digits // are prime int countnumber( int L, int R) { vector< bool > prime(651, true ); primesieve(prime); int cnt = 0; // Iterate for each value // in the range of L to R for ( int i = L; i <= R; i++) { // digit.first stores sum of digits // digit.second stores sum of // square of digit pair< int , int > digit = sum_sqsum(i); // If sum of digits and sum of // square of digit both are // prime then increment the count if (prime[digit.first] && prime[digit.second]) { cnt += 1; } } return cnt; } // Driver Code int main() { int L = 10; int R = 20; cout << countnumber(L, R); } |
Java
// Java implementation of the approach import java.util.*; class GFG { static class pair { int first, second; public pair( int first, int second) { this .first = first; this .second = second; } } // Sieve of prime numbers static void primesieve( boolean []prime) { // Sieve to store whether a // number is prime or not in // O(nlog(log(n))) prime[ 1 ] = false ; for ( int p = 2 ; p * p <= 650 ; p++) { if (prime[p] == true ) { for ( int i = p * 2 ; i <= 650 ; i += p) prime[i] = false ; } } } // Function to return sum of digit // and sum of square of digit static pair sum_sqsum( int n) { int sum = 0 ; int sqsum = 0 ; int x; // Until number is not // zero while (n > 0 ) { x = n % 10 ; sum += x; sqsum += x * x; n /= 10 ; } return ( new pair(sum, sqsum)); } // Function to return the count // of number form L to R // whose sum of digits and // sum of square of digits // are prime static int countnumber( int L, int R) { boolean []prime = new boolean [ 651 ]; Arrays.fill(prime, true ); primesieve(prime); int cnt = 0 ; // Iterate for each value // in the range of L to R for ( int i = L; i <= R; i++) { // digit.first stores sum of digits // digit.second stores sum of // square of digit pair digit = sum_sqsum(i); // If sum of digits and sum of // square of digit both are // prime then increment the count if (prime[digit.first] && prime[digit.second]) { cnt += 1 ; } } return cnt; } // Driver Code public static void main(String[] args) { int L = 10 ; int R = 20 ; System.out.println(countnumber(L, R)); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approach from math import sqrt # Sieve of prime numbers def primesieve(prime) : # Sieve to store whether a # number is prime or not in # O(nlog(log(n))) prime[ 1 ] = False ; for p in range ( 2 , int (sqrt( 650 )) + 1 ) : if (prime[p] = = True ) : for i in range (p * 2 , 651 , p) : prime[i] = False ; # Function to return sum of digit # and sum of square of digit def sum_sqsum(n) : sum = 0 ; sqsum = 0 ; # Until number is not # zero while (n) : x = n % 10 ; sum + = x; sqsum + = x * x; n / / = 10 ; return ( sum , sqsum); # Function to return the count # of number form L to R # whose sum of digits and # sum of square of digits # are prime def countnumber(L, R): prime = [ True ] * 651 ; primesieve(prime); cnt = 0 ; # Iterate for each value # in the range of L to R for i in range (L, R + 1 ) : # digit.first stores sum of digits # digit.second stores sum of # square of digit digit = sum_sqsum(i); # If sum of digits and sum of # square of digit both are # prime then increment the count if (prime[digit[ 0 ]] and prime[digit[ 1 ]]) : cnt + = 1 ; return cnt; # Driver Code if __name__ = = "__main__" : L = 10 ; R = 20 ; print (countnumber(L, R)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { public class pair { public int first, second; public pair( int first, int second) { this .first = first; this .second = second; } } // Sieve of prime numbers static void primesieve( bool []prime) { // Sieve to store whether a // number is prime or not in // O(nlog(log(n))) prime[1] = false ; for ( int p = 2; p * p <= 650; p++) { if (prime[p] == true ) { for ( int i = p * 2; i <= 650; i += p) prime[i] = false ; } } } // Function to return sum of digit // and sum of square of digit static pair sum_sqsum( int n) { int sum = 0; int sqsum = 0; int x; // Until number is not // zero while (n > 0) { x = n % 10; sum += x; sqsum += x * x; n /= 10; } return ( new pair(sum, sqsum)); } // Function to return the count // of number form L to R // whose sum of digits and // sum of square of digits // are prime static int countnumber( int L, int R) { bool []prime = new bool [651]; for ( int i = 0; i < 651; i++) prime[i] = true ; primesieve(prime); int cnt = 0; // Iterate for each value // in the range of L to R for ( int i = L; i <= R; i++) { // digit.first stores sum of digits // digit.second stores sum of // square of digit pair digit = sum_sqsum(i); // If sum of digits and sum of // square of digit both are // prime then increment the count if (prime[digit.first] && prime[digit.second]) { cnt += 1; } } return cnt; } // Driver Code public static void Main(String[] args) { int L = 10; int R = 20; Console.WriteLine(countnumber(L, R)); } } // This code is contributed by 29AjayKumar |
4
Note:
-
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.
Recommended Posts:
- Find the highest occurring digit in prime numbers in a range
- Count of Numbers in Range where first digit is equal to last digit of the number
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count of Numbers in a Range where digit d occurs exactly K times
- Check if the first and last digit of number N is prime and their sum is less than K
- Queries for the smallest and the largest prime number of given digit
- Check if the first and last digit of the smallest number forms a prime
- Queries to check whether a given digit is present in the given Range
- Integers from the range that are composed of a single distinct digit
- Shortest path to reach one prime to other by changing single digit at a time
- Composite numbers with digit sum 1
- Print all n-digit strictly increasing numbers
- Check whether a number can be expressed as a product of single digit numbers
- Check if product of array containing prime numbers is a perfect square
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.