Maximum sum after repeatedly dividing N by a divisor | Python 3 implementation of the above approach ; Function to find the smallest divisor ; Function to find the maximum sum ; Driver Code
from math import sqrt NEW_LINE def smallestDivisor ( n ) : NEW_LINE INDENT mx = int ( sqrt ( n ) ) NEW_LINE for i in range ( 2 , mx + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return n NEW_LINE DEDENT def maxSum ( n ) : NEW_LINE INDENT res = n NEW_LINE while ( n > 1 ) : NEW_LINE INDENT divi = smallestDivisor ( n ) NEW_LINE n = int ( n / divi ) NEW_LINE res += n NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 34 NEW_LINE print ( maxSum ( n ) ) NEW_LINE DEDENT