Maximum sum after repeatedly dividing N by a divisor | Function to find the smallest divisor ; Function to find the maximum sum ; Driver Code
function smallestDivisor ( n ) { var mx = Math . sqrt ( n ) ; for ( i = 2 ; i <= mx ; i ++ ) if ( n % i == 0 ) return i ; return n ; } function maxSum ( n ) { var res = n ; while ( n > 1 ) { var divi = smallestDivisor ( n ) ; n /= divi ; res += n ; } return res ; } var n = 34 ; document . write ( maxSum ( n ) ) ;