- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Maximum sum after repeatedly dividing N by a divisor in C++

In this problem, we are given an integer N. Our task is to create a program that will find the maximum sum after repeatedly dividing N by a divisor in C++.

**Program description** − we will divide the number N recursively until it becomes one and then sum up all the divisors and find the maximum of all divisors.

Let’s take an example to understand the problem,

**Input** − N = 12

**Output** − 22

**Explanation** − let’s divide the number recursively and find the sum.

Division 1: 12/2 = 6 Division 2: 6/2 = 3 Division 3: 3/3 = 1 Sum = 12+6+3+1 = 22

To solve this problem, we will find the maximum sum by making the maximum of the individual values by dividing N by the smallest divisor of N.

## Example

Program to illustrate the working our solution,

#include <bits/stdc++.h> using namespace std; int smallestDivisor(int n){ int mx = sqrt(n); for (int i = 2; i <= mx; i++) if (n % i == 0) return i; return n; } int calculateMaxSum(int n) { long long maxSum = n; while (n > 1) { int divisor = smallestDivisor(n); n /= divisor; maxSum += n; } return maxSum; } int main(){ int N = 12; cout<<"The maximum sum after repeatedly dividing "<<N<<" by divisor is "<<calculateMaxSum(N); return 0; }

## Output

The maximum sum after repeatedly dividing 12 by divisor is 22

- Related Questions & Answers
- Array sum after dividing numbers from previous?
- Array sum after dividing numbers from previous in C?
- Program to find remainder after dividing n number of 1s by m in Python
- Maximum subarray sum after dividing array into subarrays based on the given queries in Java
- Maximum consecutive 1s after n swaps in JavaScript
- How to execute a task repeatedly after fixed time intervals in iOS
- How to repeat a task repeatedly after a regular interval in Swift(iOS)?
- Maximum subarray sum in O(n) using prefix sum in C++
- Maximum number of dots after throwing a dice N times in C++
- Count number of digits after decimal on dividing a number in C++
- Sum which is divisible by n in JavaScript
- How to execute Async task repeatedly after fixed time intervals in Android?
- Maximum Subarray Sum after inverting at most two elements in C++
- Maximum sum of n consecutive elements of array in JavaScript
- Find the k-th smallest divisor of a natural number N in C++

Advertisements