Given a positive number N. The task is to find maximum sum of distinct numbers such that the LCM of all these numbers is equal to N.**Examples:**

Input : 2 Output : 3 The distinct numbers you can have are just 1 and 2 and their sum is equal to 3. Input : 5 Output : 6

As the LCM of all the numbers is N. So all the numbers must be the divisors of N and all the numbers are distinct so answer must be the sum of all the divisors of N. To find all the divisors efficiently refer to article https://www.geeksforgeeks.org/find-all-divisors-of-a-natural-number-set-2/

## C++

`// C++ program to find the max sum of` `// numbers whose lcm is n` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// Returns maximum sum of numbers with` `// LCM as N` `int` `maxSumLCM(` `int` `n)` `{` ` ` `int` `max_sum = 0; ` `// Initialize result` ` ` `// Finding a divisor of n and adding` ` ` `// it to max_sum` ` ` `for` `(` `int` `i=1; i*i<=n; i++)` ` ` `{` ` ` `if` `(n%i == 0)` ` ` `{` ` ` `max_sum += i;` ` ` `if` `(n/i != i)` ` ` `max_sum += (n/i);` ` ` `}` ` ` `}` ` ` `return` `max_sum;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `n = 2;` ` ` `cout << MaxSumLCM(n) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java program to find the max sum of` `// numbers whose lcm is n` `class` `MaxSum` `{` ` ` `// Returns maximum sum of numbers with` ` ` `// LCM as N` ` ` `static` `int` `maxSumLCM(` `int` `n)` ` ` `{` ` ` `int` `max_sum = ` `0` `; ` `// Initialize result` ` ` ` ` `// Finding a divisor of n and adding` ` ` `// it to max_sum` ` ` `for` `(` `int` `i=` `1` `; i*i<=n; i++)` ` ` `{` ` ` `if` `(n%i == ` `0` `)` ` ` `{` ` ` `max_sum += i;` ` ` `if` `(n/i != i)` ` ` `max_sum += (n/i);` ` ` `}` ` ` `}` ` ` ` ` `return` `max_sum;` ` ` `}` ` ` ` ` `// main function` ` ` `public` `static` `void` `main (String[] args)` ` ` `{` ` ` `int` `n = ` `2` `;` ` ` `System.out.println(maxSumLCM(n));` ` ` `}` `}` |

## Python3

`# Python3 program to find the max sum of` `# numbers whose lcm is n` `# Returns maximum sum of numbers with` `# LCM as N` `def` `maxSumLCM(n) :` ` ` ` ` `# Initialize result` ` ` `max_sum ` `=` `0` ` ` `# Finding a divisor of n and adding` ` ` `# it to max_sum` ` ` `i ` `=` `1` ` ` `while` `(i ` `*` `i<` `=` `n ):` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `) :` ` ` `max_sum ` `=` `max_sum ` `+` `i` ` ` `if` `(n ` `/` `/` `i !` `=` `i) :` ` ` `max_sum ` `=` `max_sum ` `+` `(n ` `/` `/` `i)` ` ` `i ` `=` `i ` `+` `1` ` ` ` ` `return` `max_sum` `# Driver code` `n ` `=` `2` `print` `(maxSumLCM(n))` `# This code is contributed by Nikita Tiwari.` |

## C#

`// C# program to find the max sum` `// of numbers whose lcm is n` `using` `System;` `class` `MaxSum` `{` ` ` ` ` `// Returns maximum sum of ` ` ` `// numbers with LCM as N` ` ` `static` `int` `maxSumLCM(` `int` `n)` ` ` `{` ` ` ` ` `// Initialize result` ` ` `int` `max_sum = 0;` ` ` ` ` `// Finding a divisor of n and` ` ` `// adding it to max_sum` ` ` `for` `(` `int` `i = 1; i * i <= n; i++)` ` ` `{` ` ` `if` `(n % i == 0)` ` ` `{` ` ` `max_sum += i;` ` ` `if` `(n / i != i)` ` ` `max_sum += (n / i);` ` ` `}` ` ` `}` ` ` ` ` `return` `max_sum;` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `Main (String[] args)` ` ` `{` ` ` `int` `n = 2;` ` ` `Console.Write(maxSumLCM(n));` ` ` `}` `}` `// This code is contributed by parashar..` |

## PHP

`<?php` `// PHP program to find` `// the max sum of numbers` `// whose lcm is n` `// Returns maximum sum` `// of numbers with` `// LCM as N` `function` `maxSumLCM(` `$n` `)` `{` ` ` `// Initialize result` ` ` `$max_sum` `= 0;` ` ` `// Finding a divisor` ` ` `// of n and adding` ` ` `// it to max_sum` ` ` `for` `(` `$i` `= 1;` ` ` `$i` `* ` `$i` `<= ` `$n` `; ` `$i` `++)` ` ` `{` ` ` `if` `(` `$n` `% ` `$i` `== 0)` ` ` `{` ` ` `$max_sum` `+= ` `$i` `;` ` ` `if` `(` `$n` `/ ` `$i` `!= ` `$i` `)` ` ` `$max_sum` `+= (` `$n` `/ ` `$i` `);` ` ` `}` ` ` `}` ` ` `return` `$max_sum` `;` `}` `// Driver code` `$n` `= 2;` `echo` `MaxSumLCM(` `$n` `);` ` ` `// This code is contributed` `// by ajit` `?>` |

## Javascript

`<script>` `// Javascript program to find the max sum of` `// numbers whose lcm is n` `// Returns maximum sum of numbers with` `// LCM as N` `function` `maxSumLCM(n)` `{` ` ` `let max_sum = 0; ` `// Initialize result` ` ` `// Finding a divisor of n and adding` ` ` `// it to max_sum` ` ` `for` `(let i=1; i*i<=n; i++)` ` ` `{` ` ` `if` `(n%i == 0)` ` ` `{` ` ` `max_sum += i;` ` ` `if` `(n/i != i)` ` ` `max_sum += (n/i);` ` ` `}` ` ` `}` ` ` `return` `max_sum;` `}` `// Driver code` ` ` ` ` `let n = 2;` ` ` `document.write(maxSumLCM(n) + ` `"<br>"` `);` ` ` `// This code is contributed by Mayank Tyagi` `</script>` |

**Output:**

3

This article is contributed by **Ayush Jha**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**