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
< ? php function maxPrimeFactors ( $ n ) { $ num = $ n ; $ maxPrime = -1 ; while ( $ n % 2 == 0 ) { $ maxPrime = 2 ; $ n /= 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { while ( $ n % $ i == 0 ) { $ maxPrime = $ i ; $ n = $ n / $ i ; } } if ( $ n > 2 ) $ maxPrime = $ n ; $ sum = $ maxPrime + $ num ; return $ sum ; } $ n = 19 ; echo maxPrimeFactors ( $ n ) ; ? >