Queries on the sum of prime factor counts in a range | Javascript program to find sum prime factors in given range . ; using sieve method to evaluating the prime factor of numbers ; if i is prime ; setting number of prime factor of a prime number . ; Returns sum of counts of prime factors in range from l to r . This function mainly uses count which is filled by Sieve ( ) ; finding the sum of number of prime factor of numbers in a range . ; Driver code
var MAX = 1000006 ; function sieve ( count ) { for ( i = 2 ; i * i <= MAX ; i ++ ) { if ( count [ i ] == 0 ) { for ( j = 2 * i ; j < MAX ; j += i ) count [ j ] ++ ; count [ i ] = 1 ; } } } function query ( count , l , r ) { var sum = 0 ; for ( i = l ; i <= r ; i ++ ) sum += count [ i ] ; return sum ; } var count = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; sieve ( count ) ; document . write ( query ( count , 6 , 10 ) + " " + query ( count , 1 , 5 ) ) ;