📜  在 n 的所有除数中找到最大的数字总和

📅  最后修改于: 2022-05-13 01:57:05.614000             🧑  作者: Mango

在 n 的所有除数中找到最大的数字总和

给定一个整数 n,找出 n 的所有除数中的最大数字和。
例子 :

Input : n = 12 
Output : 6
Explanation:
The divisors are: 1 2 3 4 6 12.
6 is maximum sum among all divisors

Input : n = 68
Output : 14
Explanation: 
The divisors are: 1 2 4 68
68 consists of maximum sum of digit

天真的方法:
这个想法很简单,我们一一找到一个数字的所有除数。对于每个除数,我们计算数字总和。最后,我们返回最大的数字总和。
时间复杂度: O(n log n)
以下是上述方法的实现:

CPP
// CPP program to find maximum 
// sum of digits in all divisors
// of n numbers.
#include 
using namespace std;
 
// Function to get sum of digits
int getSum(int n)
{
int sum = 0;
while (n != 0)
{
    sum = sum + n % 10;
    n = n/10;
}
return sum;
}
 
// returns maximum sum
int largestDigitSumdivisior(int n)
{
    int res = 0;
    for (int i = 1; i <= n; i++)
 
        // if i is factor of n
        // then push the divisor
        // in the stack.
        if (n % i == 0)
        res = max(res, getSum(i));
 
    return res;
}
 
// Driver Code
int main()
{
    int n = 14;
    cout << largestDigitSumdivisior(n)
         << endl;
    return 0;
}


Java
// Java program to find maximum
// sum of digits in all divisors
// of n numbers.
import java.util.*;
import java.lang.*;
 
class GfG
{
     
    // Function to get
    // sum of digits
    public static int getSum(int n)
    {
        int sum = 0;
        while (n != 0)
        {
            sum = sum + n % 10;
            n = n/10;
        }
        return sum;
    }
 
    // returns maximum sum
    public static int largestDigitSumdivisior(int n)
    {
        int res = 0;
        for (int i = 1; i <= n; i++)
 
            // if i is factor of n 
            // then push the divisor
            // in the stack.
            if (n % i == 0)
            res = Math.max(res, getSum(i));
 
        return res;
    }
     
    // Driver Code
    public static void main(String argc[]){
        int n = 14;
         
        System.out.println(largestDigitSumdivisior(n));
    }
     
}
// This code is contributed
// by Sagar Shukla


Python3
# Python3 code to find
# maximum sum of digits
# in all divisors of n numbers.
 
# Function to get sum of digits
def getSum( n ):
    sum = 0
    while n != 0:
        sum = sum + n % 10
        n = int( n / 10 )
    return sum
 
# returns maximum sum
def largestDigitSumdivisior( n ):
    res = 0
    for i in range(1, n + 1):
 
        # if i is factor of n
        # then push the divisor
        # in the stack.
        if n % i == 0:
            res = max(res, getSum(i))
 
    return res
 
 
# Driver Code
n = 14
print(largestDigitSumdivisior(n) )
 
# This code is contributed
# by "Sharad_Bhardwaj".


C#
// C# program to find maximum
// sum of digits in all
// divisors of n numbers.
using System;
 
class GfG
{
     
    // Function to get
    // sum of digits
    public static int getSum(int n)
    {
        int sum = 0;
        while (n != 0)
        {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
    }
 
    // returns maximum sum
    public static int largestDigitSumdivisior(int n)
    {
        int res = 0;
        for (int i = 1; i <= n; i++)
 
            // if i is factor of n
            // then push the divisor
            // in the stack.
            if (n % i == 0)
            res = Math.Max(res, getSum(i));
 
        return res;
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 14;
         
        Console.WriteLine(largestDigitSumdivisior(n));
    }
     
}
 
// This code is contributed by vt_m


PHP


Javascript


CPP
// CPP program to find
// maximum sum of digits
// in all divisors of n
// numbers.
#include 
using namespace std;
 
// Function to get
// sum of digits
int getSum(int n)
{
int sum = 0;
while (n != 0)
{
    sum = sum + n % 10;
    n = n / 10;
}
return sum;
}
 
// returns maximum sum
int largestDigitSumdivisior(int n)
{
    int res = 0;
     
    // traverse till sqrt(n)
    for (int i = 1; i <= sqrt(n); i++)
 
        // if i is factor of
        // n then push the
        // divisor in the stack.
        if (n % i == 0)
        {
            // check for both the divisors
            res = max(res, getSum(i));
            res = max(res,getSum(n / i));
        }    
 
    return res;
}
 
// Driver Code
int main()
{
    int n = 14;
    cout << largestDigitSumdivisior(n)
         << endl;
    return 0;
}


Java
// Java program to find maximum
// sum of digits in all divisors
// of n numbers.
 
import java.io.*;
import java.math.*;
 
class GFG
{
 
    // Function to get
    // sum of digits
    static int getSum(int n)
    {
        int sum = 0;
        while (n != 0)
        {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
    }
 
    // returns maximum sum
    static int largestDigitSumdivisior(int n)
    {
        int res = 0;
 
        // traverse till sqrt(n)
        for (int i = 1; i <= Math.sqrt(n); i++)
        {
 
            // if i is factor of
            // n then push the
            // divisor in the stack.
            if (n % i == 0)
            {
                 
                // check for both the divisors
                res = Math.max(res, getSum(i));
                res = Math.max(res, getSum(n / i));
            }
 
        }
         
        return res;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 14;
        System.out.println(largestDigitSumdivisior(n));
    }
}
 
// This code is contributed
// by Nikita Tiwari


Python3
# Python 3 program
# to find maximum
# sum of digits in
# all divisors of
# n numbers
import math
 
# Function to get
# sum of digits
def getSum(n) :
    sm = 0
    while (n != 0) :
        sm = sm + n % 10
        n = n // 10
         
    return sm
     
     
# returns maximum sum
def largestDigitSumdivisior(n) :
    res = 0
     
    # traverse till sqrt(n)
    for i in range(1, (int)(math.sqrt(n))+1) :
         
        # if i is factor of n then
        # push the divisor in the
        # stack.
        if (n % i == 0) :
 
            # check for both the
            # divisors
            res = max(res, getSum(i))
            res = max(res, getSum(n // i))
             
    return res
 
# Driver Code
n = 14
print(largestDigitSumdivisior(n))
 
#This code is contributed
# by Nikita Tiwari


C#
// C# program to find maximum sum
// of digits in all divisors of n
// numbers.
using System;
 
class GFG
{
 
    // Function to get
    // sum of digits
    static int getSum(int n)
    {
        int sum = 0;
         
        while (n != 0)
        {
            sum = sum + n % 10;
            n = n / 10;
        }
         
        return sum;
    }
 
    // returns maximum sum
    static int largestDigitSumdivisior(int n)
    {
        int res = 0;
 
        // traverse till sqrt(n)
        for (int i = 1; i <= Math.Sqrt(n); i++)
        {
 
            // if i is factor of n then push the
            // divisor in the stack.
            if (n % i == 0)
            {
                 
                // check for both the divisors
                res = Math.Max(res, getSum(i));
                res = Math.Max(res, getSum(n / i));
            }
 
        }
         
        return res;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 14;
         
        Console.WriteLine(largestDigitSumdivisior(n));
    }
}
 
// This code is contributed by Vt_m


PHP


Javascript


输出 :

7

一种有效的方法是在 O(sqrt n) 中找到除数。我们遵循与上述相同的步骤,只需迭代直到 sqrt(n) 并在 n%i==0 时将 i 和 n/i 作为除数。
下面是上述方法的实现:

CPP

// CPP program to find
// maximum sum of digits
// in all divisors of n
// numbers.
#include 
using namespace std;
 
// Function to get
// sum of digits
int getSum(int n)
{
int sum = 0;
while (n != 0)
{
    sum = sum + n % 10;
    n = n / 10;
}
return sum;
}
 
// returns maximum sum
int largestDigitSumdivisior(int n)
{
    int res = 0;
     
    // traverse till sqrt(n)
    for (int i = 1; i <= sqrt(n); i++)
 
        // if i is factor of
        // n then push the
        // divisor in the stack.
        if (n % i == 0)
        {
            // check for both the divisors
            res = max(res, getSum(i));
            res = max(res,getSum(n / i));
        }    
 
    return res;
}
 
// Driver Code
int main()
{
    int n = 14;
    cout << largestDigitSumdivisior(n)
         << endl;
    return 0;
}

Java

// Java program to find maximum
// sum of digits in all divisors
// of n numbers.
 
import java.io.*;
import java.math.*;
 
class GFG
{
 
    // Function to get
    // sum of digits
    static int getSum(int n)
    {
        int sum = 0;
        while (n != 0)
        {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
    }
 
    // returns maximum sum
    static int largestDigitSumdivisior(int n)
    {
        int res = 0;
 
        // traverse till sqrt(n)
        for (int i = 1; i <= Math.sqrt(n); i++)
        {
 
            // if i is factor of
            // n then push the
            // divisor in the stack.
            if (n % i == 0)
            {
                 
                // check for both the divisors
                res = Math.max(res, getSum(i));
                res = Math.max(res, getSum(n / i));
            }
 
        }
         
        return res;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 14;
        System.out.println(largestDigitSumdivisior(n));
    }
}
 
// This code is contributed
// by Nikita Tiwari

Python3

# Python 3 program
# to find maximum
# sum of digits in
# all divisors of
# n numbers
import math
 
# Function to get
# sum of digits
def getSum(n) :
    sm = 0
    while (n != 0) :
        sm = sm + n % 10
        n = n // 10
         
    return sm
     
     
# returns maximum sum
def largestDigitSumdivisior(n) :
    res = 0
     
    # traverse till sqrt(n)
    for i in range(1, (int)(math.sqrt(n))+1) :
         
        # if i is factor of n then
        # push the divisor in the
        # stack.
        if (n % i == 0) :
 
            # check for both the
            # divisors
            res = max(res, getSum(i))
            res = max(res, getSum(n // i))
             
    return res
 
# Driver Code
n = 14
print(largestDigitSumdivisior(n))
 
#This code is contributed
# by Nikita Tiwari

C#

// C# program to find maximum sum
// of digits in all divisors of n
// numbers.
using System;
 
class GFG
{
 
    // Function to get
    // sum of digits
    static int getSum(int n)
    {
        int sum = 0;
         
        while (n != 0)
        {
            sum = sum + n % 10;
            n = n / 10;
        }
         
        return sum;
    }
 
    // returns maximum sum
    static int largestDigitSumdivisior(int n)
    {
        int res = 0;
 
        // traverse till sqrt(n)
        for (int i = 1; i <= Math.Sqrt(n); i++)
        {
 
            // if i is factor of n then push the
            // divisor in the stack.
            if (n % i == 0)
            {
                 
                // check for both the divisors
                res = Math.Max(res, getSum(i));
                res = Math.Max(res, getSum(n / i));
            }
 
        }
         
        return res;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 14;
         
        Console.WriteLine(largestDigitSumdivisior(n));
    }
}
 
// This code is contributed by Vt_m

PHP


Javascript


输出 :

7

时间复杂度: O(sqrt(n) log n)