Maximum sum after repeatedly dividing N by a divisor | Java implementation of the above approach ; Function to find the smallest divisor ; Function to find the maximum sum ; Driver Code
import java . io . * ; class GFG { static double smallestDivisor ( int n ) { double mx = Math . sqrt ( n ) ; for ( int i = 2 ; i <= mx ; i ++ ) if ( n % i == 0 ) return i ; return n ; } static double maxSum ( int n ) { long res = n ; while ( n > 1 ) { double divi = smallestDivisor ( n ) ; n /= divi ; res += n ; } return res ; } public static void main ( String [ ] args ) { int n = 34 ; System . out . println ( maxSum ( n ) ) ; } }