Find largest sum of digits in all divisors of n
Last Updated :
19 Jul, 2025
Given an integer number n, find the 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 the largest sum of digits.
Below is the implementation of the above approach:
CPP
// 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;
}
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
<?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.
?>
JavaScript
<script>
// Javascript program to find maximum
// sum of digits in all divisors
// of n numbers.
// Function to get sum of digits
function getSum(n)
{
let sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = Math.floor(n/10);
}
return sum;
}
// returns maximum sum
function largestDigitSumdivisior(n)
{
let res = 0;
for (let 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
let n = 14;
document.write(largestDigitSumdivisior(n)
+ "<br>");
// This code is contributed by Mayank Tyagi
</script>
Time Complexity: O(n*log10 (n))
Auxiliary Space: O(1)
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
// 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;
}
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
<?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.
?>
JavaScript
<script>
// JavaScript program to find
// maximum sum of digits
// in all divisors of n
// numbers.
// Function to get
// sum of digits
function getSum(n)
{
var sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = parseInt(n / 10);
}
return sum;
}
// returns maximum sum
function largestDigitSumdivisior(n)
{
var res = 0;
// traverse till sqrt(n)
for (var 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
var n = 14;
document.write(largestDigitSumdivisior(n));
</script>
Time Complexity: O(sqrt(n) log n)
Auxiliary Space: O(1) as it is using constant space for variables
Similar Reads
Find sum of divisors of all the divisors of a natural number Given a natural number n, the task is to find sum of divisors of all the divisors of n. Examples: Input : n = 54 Output : 232 Divisors of 54 = 1, 2, 3, 6, 9, 18, 27, 54. Sum of divisors of 1, 2, 3, 6, 9, 18, 27, 54 are 1, 3, 4, 12, 13, 39, 40, 120 respectively. Sum of divisors of all the divisors of
7 min read
Check if the sum of digits of a number divides it Given a number n, the task is to check if the sum of digits of the given number divides the number or not. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12.Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.Using Iterative Method - O(
6 min read
Check if the sum of digits of a number divides it Given a number n, the task is to check if the sum of digits of the given number divides the number or not. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12.Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.Using Iterative Method - O(
6 min read
Sum of all divisors from 1 to n Given a positive integer n. Find the value of \sum_{i=1}^{i=n} F(i) where function F(i) for number i be defined as the sum of all divisors of 'i'. Examples : Input: 4Output: 15ExplanationF(1) = 1F(2) = 1 + 2 = 3F(3) = 1 + 3 = 4F(4) = 1 + 2 + 4 = 7ans = F(1) + F(2) + F(3) + F(4) = 1 + 3 + 4 + 7 = 15I
15+ min read
Sum of all proper divisors of natural numbers in an array Given an array of natural numbers count the sum of its proper divisors for every element in array. Example: Input : int arr[] = {8, 13, 24, 36, 59, 75, 87} Output : 7 1 36 55 1 49 21 Number 8 has 3 proper divisors 1, 2, 4 and their sum comes out to be 7.Recommended PracticeSum of Divisors in an arra
9 min read
Find the sum of the number of divisors Given three integers A, B, C, the task is to find ?Ai=1 ?Bj=1?Ck=1 d(i.j.k), where d(x) is the number of divisors of x. Answer can be very large, So, print answer modulo 109+7. Examples: Input: A = 2, B = 2, c = 2 Output: 20 Explanation: d(1.1.1) = d(1) = 1; d(1·1·2) = d(2) = 2; d(1·2·1) = d(2) = 2;
6 min read