Maximum sum after repeatedly dividing N by a divisor | C ++ implementation of the above approach ; Function to find the smallest divisor ; Function to find the maximum sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestDivisor ( int n ) { int mx = sqrt ( n ) ; for ( int i = 2 ; i <= mx ; i ++ ) if ( n % i == 0 ) return i ; return n ; } int maxSum ( int n ) { long long res = n ; while ( n > 1 ) { int divi = smallestDivisor ( n ) ; n /= divi ; res += n ; } return res ; } int main ( ) { int n = 34 ; cout << maxSum ( n ) ; return 0 ; }