Find largest sum of digits in all divisors of n

Given a integer number n, find largest sum of digits in all divisors of n.

Examples :

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

Naive approach:
The idea is simple, we find all divisors of a number one by one. For every divisor, we compute sum of digits. Finally, we return largest sum of digits.

Time Complexity: O(n log n)

Below is the implementation of above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find maximum  
// sum of digits in all divisors
// of n numbers.
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find maximum
// sum of digits in all
// divisors of n numbers.
  
// Function to get 
// sum of digits 
function getSum( $n)
    $sum = 0;
    while ($n != 0)
{
    $sum = $sum + $n % 10;
    $n = $n/10;
}
return $sum;
}
  
// returns maximum sum
function largestDigitSumdivisior( $n)
{
    $res = 0;
    for ($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
    $n = 14;
    echo largestDigitSumdivisior($n);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output :

7

An efficient approach will be to find the divisors in O(sqrt n). We follow the same steps as above , just iterate till sqrt(n) and get i and n/i as their divisors whenever n%i==0.

Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find
// maximum sum of digits
// in all divisors of n 
// numbers.
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find maximum
// sum of digits in all
// divisors of n numbers
  
// Function to get
// sum of digits 
function getSum($n)
    $sum = 0;
while ($n != 0)
{
    $sum = $sum + $n % 10;
    $n = $n / 10;
}
return $sum;
}
  
// returns maximum sum
function largestDigitSumdivisior( $n)
{
    $res = 0;
      
    // traverse till sqrt(n) 
    for ($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
$n = 14;
echo largestDigitSumdivisior($n);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output :

7

Time Complexity: O(sqrt(n) log n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Striver, vt_m