Sum of numbers in a range [ L , R ] whose count of divisors is prime | Javascript implementation of above approach ; prime [ i ] stores 1 if i is prime ; divi [ i ] stores the count of divisors of i ; sum [ i ] will store the sum of all the integers from 0 to i whose count of divisors is prime ; Function for Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be 0 if i is Not a prime , else true . ; 0 and 1 is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count the divisors ; For each number i we will go to each of the multiple of i and update the count of divisor of its multiple j as i is one of the factor of j ; Function for pre - computation ; If count of divisors of i is prime ; taking prefix sum ; Driver code ; Find all the prime numbers till N ; Update the count of divisors of all the numbers till N ; Precomputation for the prefix sum array ; Perform query
var N = 100000 ; var prime = Array . from ( { length : N } , ( _ , i ) => 0 ) ; var divi = Array . from ( { length : N } , ( _ , i ) => 0 ) ; var sum = Array . from ( { length : N } , ( _ , i ) => 0 ) ; function SieveOfEratosthenes ( ) { for ( i = 0 ; i < N ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( p = 2 ; p * p < N ; p ++ ) { if ( prime [ p ] == 1 ) { for ( i = p * p ; i < N ; i += p ) prime [ i ] = 0 ; } } } function DivisorCount ( ) { for ( i = 1 ; i < N ; i ++ ) { for ( j = i ; j < N ; j += i ) { divi [ j ] ++ ; } } } function pre ( ) { for ( i = 1 ; i < N ; i ++ ) { if ( prime [ divi [ i ] ] == 1 ) { sum [ i ] = i ; } } for ( i = 1 ; i < N ; i ++ ) sum [ i ] += sum [ i - 1 ] ; } var l = 5 , r = 8 ; SieveOfEratosthenes ( ) ; DivisorCount ( ) ; pre ( ) ; document . write ( sum [ r ] - sum [ l - 1 ] ) ;