Traversing through all prime factors .
var res = 1 ; for ( var i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { var count = 0 , curr_sum = 1 ; var curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = parseInt ( n / i ) ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; }