Maximum sum of distinct numbers with LCM as N | C / C ++ program to get maximum sum of Numbers with condition that their LCM should be N ; Method returns maximum sum f distinct number whose LCM is N ; find all divisors which divides ' N ' ; if ' i ' is divisor of ' N ' ; if both divisors are same then add it only once else add both ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMaximumSumWithLCMN ( int N ) { int sum = 0 ; int LIM = sqrt ( N ) ; for ( int i = 1 ; i <= LIM ; i ++ ) { if ( N % i == 0 ) { if ( i == ( N / i ) ) sum += i ; else sum += ( i + N / i ) ; } } return sum ; } int main ( ) { int N = 12 ; cout << getMaximumSumWithLCMN ( N ) << endl ; return 0 ; }