Sum of all divisors from 1 to N | Set 3 | Javascript program implementation of the approach ; Function to find the sum of all divisors of all numbers from 1 to N ; Stores the sum ; Marks the last polet of occurence with same count ; Calculate the sum ; Return the result ; Driver Code
let m = 1000000007 ; function solve ( n ) { let s = 0 ; for ( let l = 1 ; l <= n ; ) { let r = ( n / Math . floor ( n / l ) ) ; let x = Math . floor ( ( ( r % m ) * ( ( r + 1 ) % m ) ) / 2 ) % m ; let y = Math . floor ( ( ( l % m ) * ( ( l - 1 ) % m ) ) / 2 ) % m ; let p = ( Math . floor ( n / l ) % m ) ; s = ( s + ( ( ( x - y ) % m ) * p ) % m + m ) % m ; s %= m ; l = r + 1 ; } document . write ( ( s + m ) % m ) ; } let n = 12 ; solve ( n ) ;