Numbers with sum of digits equal to the sum of digits of its all prime factor
Last Updated :
04 Mar, 2023
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.
Examples:
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 factor for 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 the 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:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
int spf[MAXN] = { 0 };
int sum_digits[MAXN] = { 0 };
bool isValid[MAXN] = { 0 };
int ans[MAXN] = { 0 };
void Smallest_prime_factor()
{
for ( int i = 1; i < MAXN; i++)
spf[i] = i;
for ( int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for ( int i = 3; i * i <= MAXN; i += 2)
if (spf[i] == i)
for ( int j = i * i; j < MAXN; j += i)
if (spf[j] == j)
spf[j] = i;
}
int Digit_Sum( int copy)
{
int d = 0;
while (copy) {
d += copy % 10;
copy /= 10;
}
return d;
}
void Sum_Of_All_Digits()
{
for ( int n = 2; n < MAXN; n++) {
sum_digits[n] = sum_digits[n / spf[n]]
+ Digit_Sum(spf[n]);
if (Digit_Sum(n) == sum_digits[n])
isValid[n] = true ;
}
for ( int n = 2; n < MAXN; n++) {
if (isValid[n])
ans[n] = 1;
ans[n] += ans[n - 1];
}
}
int main()
{
Smallest_prime_factor();
Sum_Of_All_Digits();
int l, r;
l = 2, r = 3;
cout << "Valid numbers in the range " << l << " "
<< r << " are " << ans[r] - ans[l - 1] << endl;
l = 2, r = 10;
cout << "Valid numbers in the range " << l << " "
<< r << " are " << ans[r] - ans[l - 1] << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MAXN = 100005 ;
static int spf[] = new int [MAXN];
static int sum_digits[] = new int [MAXN];
static boolean isValid[] = new boolean [MAXN];
static int ans[] = new int [MAXN];
static void Smallest_prime_factor()
{
for ( int i = 1 ; i < MAXN; i++)
spf[i] = i;
for ( int i = 4 ; i < MAXN; i += 2 )
spf[i] = 2 ;
for ( int i = 3 ;
i * i <= MAXN; i += 2 )
if (spf[i] == i)
for ( int j = i * i;
j < MAXN; j += i)
if (spf[j] == j)
spf[j] = i;
}
static int Digit_Sum( int copy)
{
int d = 0 ;
while (copy > 0 )
{
d += copy % 10 ;
copy /= 10 ;
}
return d;
}
static void Sum_Of_All_Digits()
{
for ( int n = 2 ; n < MAXN; n++)
{
sum_digits[n] = sum_digits[n / spf[n]]
+ Digit_Sum(spf[n]);
if (Digit_Sum(n) == sum_digits[n])
isValid[n] = true ;
}
for ( int n = 2 ; n < MAXN; n++)
{
if (isValid[n])
ans[n] = 1 ;
ans[n] += ans[n - 1 ];
}
}
public static void main (String[] args)
{
Smallest_prime_factor();
Sum_Of_All_Digits();
int l, r;
l = 2 ; r = 3 ;
System.out.println( "Valid numbers in the range " +
l + " " + r + " are " +
(ans[r] - ans[l - 1 ] ));
l = 2 ; r = 10 ;
System.out.println( "Valid numbers in the range " +
l + " " + r + " are " +
(ans[r] - ans[l - 1 ]));
}
}
|
Python 3
MAXN = 100005
spf = [ 0 ] * MAXN
sum_digits = [ 0 ] * MAXN
isValid = [ 0 ] * MAXN
ans = [ 0 ] * MAXN
def Smallest_prime_factor():
for i in range ( 1 , MAXN):
spf[i] = i
for i in range ( 4 , MAXN, 2 ):
spf[i] = 2
i = 3
while i * i < = MAXN:
if (spf[i] = = i):
for j in range (i * i, MAXN, i):
if (spf[j] = = j):
spf[j] = i
i + = 2
def Digit_Sum(copy):
d = 0
while (copy) :
d + = copy % 10
copy / / = 10
return d
def Sum_Of_All_Digits():
for n in range ( 2 , MAXN) :
sum_digits[n] = (sum_digits[n / / spf[n]] +
Digit_Sum(spf[n]))
if (Digit_Sum(n) = = sum_digits[n]):
isValid[n] = True
for n in range ( 2 , MAXN) :
if (isValid[n]):
ans[n] = 1
ans[n] + = ans[n - 1 ]
if __name__ = = "__main__" :
Smallest_prime_factor()
Sum_Of_All_Digits()
l = 2
r = 3
print ( "Valid numbers in the range" , l, r,
"are" , ans[r] - ans[l - 1 ])
l = 2
r = 10
print ( "Valid numbers in the range" , l, r,
"are" , ans[r] - ans[l - 1 ])
|
C#
using System;
class GFG
{
static int MAXN = 100005;
static int []spf = new int [MAXN];
static int []sum_digits = new int [MAXN];
static bool []isValid = new bool [MAXN];
static int []ans = new int [MAXN];
static void Smallest_prime_factor()
{
for ( int i = 1; i < MAXN; i++)
spf[i] = i;
for ( int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for ( int i = 3;
i * i <= MAXN; i += 2)
if (spf[i] == i)
for ( int j = i * i;
j < MAXN; j += i)
if (spf[j] == j)
spf[j] = i;
}
static int Digit_Sum( int copy)
{
int d = 0;
while (copy > 0)
{
d += copy % 10;
copy /= 10;
}
return d;
}
static void Sum_Of_All_Digits()
{
for ( int n = 2; n < MAXN; n++)
{
sum_digits[n] = sum_digits[n / spf[n]] +
Digit_Sum(spf[n]);
if (Digit_Sum(n) == sum_digits[n])
isValid[n] = true ;
}
for ( int n = 2; n < MAXN; n++)
{
if (isValid[n])
ans[n] = 1;
ans[n] += ans[n - 1];
}
}
public static void Main ()
{
Smallest_prime_factor();
Sum_Of_All_Digits();
int l, r;
l = 2; r = 3;
Console.WriteLine( "Valid numbers in the range " +
l + " " + r + " are " +
(ans[r] - ans[l - 1] ));
l = 2; r = 10;
Console.WriteLine( "Valid numbers in the range " +
l + " " + r + " are " +
(ans[r] - ans[l - 1]));
}
}
|
Javascript
<script>
var MAXN = 100005;
var spf = Array.from({length: MAXN}, (_, i) => 0);
var sum_digits = Array.from({length: MAXN}, (_, i) => 0);
var isValid = Array.from({length: MAXN}, (_, i) => false );
var ans = Array.from({length: MAXN}, (_, i) => 0);
function Smallest_prime_factor()
{
for (i = 1; i < MAXN; i++)
spf[i] = i;
for (i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (i = 3;
i * i <= MAXN; i += 2)
if (spf[i] == i)
for (j = i * i;
j < MAXN; j += i)
if (spf[j] == j)
spf[j] = i;
}
function Digit_Sum(copy)
{
var d = 0;
while (copy > 0)
{
d += copy % 10;
copy = parseInt(copy/10);
}
return d;
}
function Sum_Of_All_Digits()
{
for (n = 2; n < MAXN; n++)
{
sum_digits[n] = sum_digits[parseInt(n / spf[n])]
+ Digit_Sum(spf[n]);
if (Digit_Sum(n) == sum_digits[n])
isValid[n] = true ;
}
for (n = 2; n < MAXN; n++)
{
if (isValid[n])
ans[n] = 1;
ans[n] += ans[n - 1];
}
}
Smallest_prime_factor();
Sum_Of_All_Digits();
var l, r;
l = 2; r = 3;
document.write( "Valid numbers in the range " +
l + " " + r + " are " +
(ans[r] - ans[l - 1] ));
l = 2; r = 10;
document.write( "<br>Valid numbers in the range " +
l + " " + r + " are " +
(ans[r] - ans[l - 1]));
</script>
|
Output:
Valid numbers in the range 2 3 are 2
Valid numbers in the range 2 10 are 5
Time Complexity: O(n log n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...