Sum of all odd factors of numbers in the range [ l , r ] | Javascript implementation of the approach ; Function to calculate the prefix sum of all the odd factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the odd factors of the numbers in the given range ; Driver code
var MAX = 100001 ; prefix = Array ( MAX ) . fill ( 0 ) function sieve_modified ( ) { for ( var i = 1 ; i < MAX ; i += 2 ) { for ( var j = i ; j < MAX ; j += i ) prefix [ j ] += i ; } for ( var i = 1 ; i < MAX ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; } function sumOddFactors ( L , R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } sieve_modified ( ) ; var l = 6 , r = 10 ; document . write ( sumOddFactors ( l , r ) ) ;