
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find largest sum of digits in all divisors of n in C++
In this problem, we are given an integer n. Our task is to find the largest sum of digits in all divisors of n.
Problem Description: Here, we will find the divisor of the number n whose sum of digits in largest.
Let’s take an example to understand the problem,
Input: 18
Output: 9
Explanation:
All divisors of 18 are 1, 2, 3, 6, 9, 18.
The maximum digits sum is 9.
Solution Approach
Find all divisors of the number N. And then find the sum of digits of each divisors and then return the value with the largest sum.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int calcDigitSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } int largestDigitSumdivisior(int n) { int maxSum = 0; for (int i = 1; i <= n; i++) if (n % i == 0) maxSum = max(maxSum, calcDigitSum(i)); return maxSum; } int main() { int n = 45; cout<<"The divisor with largest sum of digits is "<<largestDigitSumdivisior(n)<<endl; return 0; }
Output
The divisor with largest sum of digits is 9
The solution can be made more effective using modifying the method to find the divisor and making it more effective.
In this problem, we will iterate till sqrt(n) and find all divisors and other divisors are calculated using n/div. This reduces the time complexity to find divisors to sqrt(n).
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int calcDigitSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } return sum; } int largestDigitSumdivisior(int n) { int maxSum = 0; for (int i = 1; i*i <= n; i++) { if (n % i == 0) { maxSum = max(maxSum, calcDigitSum(i)); maxSum = max(maxSum,calcDigitSum(n/i)); } } return maxSum; } int main() { int n = 32; cout<<"The divisor with largest sum of digits is "<<largestDigitSumdivisior(n)<<endl; return 0; }
Output
The divisor with largest sum of digits is 8
- Related Articles
- Find sum of divisors of all the divisors of a natural number in C++
- Find the largest good number in the divisors of given number N in C++
- Find the Largest number with given number of digits and sum of digits in C++
- Compute sum of digits in all numbers from 1 to n
- Find the number of divisors of all numbers in the range [1, n] in C++
- Sum of all proper divisors of a natural number in java
- Find largest number smaller than N with same set of digits in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Largest product of n contiguous digits of a number in JavaScript
- Divisors of n-square that are not divisors of n in C++ Program
- Program to find the sum of all digits of given number in Python
- Find all divisors of a natural number in java
- Sum of the digits of a number N written in all bases from 2 to N/2 in C++
- Find last two digits of sum of N factorials using C++.
- Queries to Print All the Divisors of n using C++
