📜  不同数字的最大和,以使这些数字的LCM为N

📅  最后修改于: 2021-04-26 19:13:46             🧑  作者: Mango

给定正数N。任务是找到最大不同数字的总和,以使所有这些数字的LCM等于N。
例子:

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

由于所有数字的LCM是N。因此,所有数字必须是N的除数,并且所有数字都是唯一的,因此答案必须是N的所有除数的和。要有效地找到所有除数,请参阅文章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
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


Javascript


输出:

3