text: Sum of all divisors from 1 to N | Set 2 | JavaScript program for the above approach ; Functions returns sum of numbers from 1 to n ; Functions returns sum of numbers from a + 1 to b ; Function returns total sum of divisors ; Stores total sum ; Finding numbers and its occurence ; Sum of product of each number and its occurence ; Driver Code
code: let mod = 1000000007 ; function linearSum ( n ) { return ( n * ( n + 1 ) / 2 ) % mod ; } function rangeSum ( b , a ) { return ( linearSum ( b ) - linearSum ( a ) ) % mod ; } function totalSum ( n ) { let result = 0 ; let i = 1 ; while ( true ) { result += rangeSum ( Math . floor ( n / i ) , Math . floor ( n / ( i + 1 ) ) ) * ( i % mod ) % mod ; result %= mod ; if ( i == n ) break ; i = Math . floor ( n / ( n / ( i + 1 ) ) ) ; } return result ; } let N = 4 ; document . write ( totalSum ( N ) + " " ) ; N = 12 ; document . write ( totalSum ( N ) ) ;