- 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

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.

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; }

The maximum sum after repeatedly dividing 12 by divisor is 22

- Related Questions & Answers
- Array sum after dividing numbers from previous in C?
- Array sum after dividing numbers from previous?
- 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 circular subarray sum in C++\n
- 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++
- Maximum consecutive 1s after n swaps in JavaScript
- Maximum Subarray Sum after inverting at most two elements in C++
- Area of squares formed by joining midpoints repeatedly in C?
- Maximum subarray sum in an array created after repeated concatenation in C++
- 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 Primes whose sum is equal to given N in C++

Advertisements