
- 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 sum of divisors of all the divisors of a natural number in C++
In this problem, we are given a natural number N. Our task is to find the sum of divisors of all the divisors of a natural number.
Let's take an example to understand the problem,
Input : N = 12 Output : 55
Explanation −
The divisors of 12 are 1, 2, 3, 4, 6, 12 Sum of divisors = (1) + (1 + 2) + (1 + 3) + (1 + 2 + 4) + (1 + 2 + 3 + 6) + (1 + 2 + 3 + 4 + 6 + 12) = 1 + 3 + 4 + 7 + 12 + 28 = 55
Solution Approach
A simple solution to the problem is using factorisation of N. Using prime factorisation, we can find the sum of divisors of all divisors. Here, we will find the prime factorisation of each element.
Example
Program to illustrate the working of our solution
#include<bits/stdc++.h> using namespace std; int findSumOfDivisorsOfDivisors(int n) { map<int, int> factorCount; for (int j=2; j<=sqrt(n); j++) { int count = 0; while (n%j == 0) { n /= j; count++; } if (count) factorCount[j] = count; } if (n != 1) factorCount[n] = 1; int sumOfDiv = 1; for (auto it : factorCount) { int power = 1; int sum = 0; for (int i=it.second+1; i>=1; i--) { sum += (i*power); power *= it.first; } sumOfDiv *= sum; } return sumOfDiv; } int main() { int n = 12; cout<<"The sum of divisors of all divisors is "<<findSumOfDivisorsOfDivisors(n); return 0; }
Output
The sum of divisors of all divisors is 55
- Related Articles
- Sum of all proper divisors of a natural number in java
- Find all divisors of a natural number in java
- Find all divisors of a natural number - Set 1 in C++
- Find all divisors of a natural number - Set 2 in C++
- Find largest sum of digits in all divisors of n in C++
- Count all perfect divisors of a number in C++
- Find the number of divisors of all numbers in the range [1, n] in C++
- Program to find out the sum of the number of divisor of the divisors in Python
- Divisors of factorials of a number in java
- Find number from its divisors in C++
- Minimum number of Square Free Divisors in C++
- Check if a number is divisible by all prime divisors of another number in C++
- Find the largest good number in the divisors of given number N in C++
- Divisors of n-square that are not divisors of n in C++ Program
- Counting divisors of a number using JavaScript

Advertisements