Sum of all divisors from 1 to n |  ; var t1 = i * ( num / i - i + 1 ) ; adding i every time it appears with numbers greater than or equal to itself var t2 = ( ( ( num / i ) * ( num / i + 1 ) ) / 2 ) - ( ( i * ( i + 1 ) ) / 2 ) ; adding numbers that appear with i and are greater than i ; Driver code
function sum_all_divisors ( num ) { var sum = 0 ; for ( var i = 1 ; i <= Math . sqrt ( num ) ; i ++ ) { sum += t1 + t2 ; } return sum ; } var n ; var sum = sum_all_divisors ( n ) ; document . write ( sum ) ;