Numbers with sum of digits equal to the sum of digits of its all prime factor | maximum size of number ; array to store smallest prime factor of number ; array to store sum of digits of a number ; boolean array to check given number is countable for required answer or not . ; prefix array to store answer ; Calculating SPF ( SmallestPrime Factor ) for every number till MAXN . ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; Function to find sum of digits in a number ; find sum of digits of all numbers up to MAXN ; add sum of digits of least prime factor and n / spf [ n ] ; if it is valid make isValid true ; prefix sum to compute answer ; Driver code ; declaration ; print answer for required range ; print answer for required range
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 ( " " + l + " " + r + " " + ( ans [ r ] - ans [ l - 1 ] ) ) ; l = 2 ; r = 10 ; document . write ( " " + l + " " + r + " " + ( ans [ r ] - ans [ l - 1 ] ) ) ;