Find sum of a number and its maximum prime factor | Function to return the sum of n and it 's largest prime factor ; Initialise maxPrime to - 1. ; n must be odd at this point , thus skip the even numbers and iterate only odd numbers ; This condition is to handle the case when n is a prime number greater than 2 ; finally return the sum . ; Driver Code
function maxPrimeFactors ( n ) { var num = n ; var maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n = parseInt ( n / 2 ) ; } for ( var i = 3 ; i <= parseInt ( Math . sqrt ( n ) ) ; i += 2 ) { while ( n % i == 0 ) { maxPrime = i ; n = parseInt ( n / i ) ; } } if ( n > 2 ) { maxPrime = n ; } var sum = maxPrime + num ; return sum ; } var n = 19 ; document . write ( maxPrimeFactors ( n ) ) ;