Given a natural number, calculate sum of all its proper divisors. A proper divisor of a natural number is the divisor that is strictly less than the number.
For example, number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.
Input : num = 10 Output: 8 // proper divisors 1 + 2 + 5 = 8 Input : num = 36 Output: 55 // proper divisors 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 = 55
This problem has very simple solution, we all know that for any number ‘num’ all its divisors are always less than and equal to ‘num/2’ and all prime factors are always less than and equal to sqrt(num). So we iterate through ‘i’ till i<=sqrt(num) and for any 'i' if it divides 'num' , then we get two divisors 'i' and 'num/i' , continuously add these divisors but for some numbers divisors 'i' and 'num/i' will same in this case just add only one divisor , e.g; num=36 so for i=6 we will get (num/i)=6 , that's why we will at 6 in the summation only once. Finally we add one as one is divisor of all natural numbers.
Please refer below post for an optimized solution and formula.
Efficient solution for sum of all the factors of a number
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of all proper divisors of natural numbers in an array
- Find sum of divisors of all the divisors of a natural number
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Sum of all proper divisors from 1 to N
- Product of proper divisors of a number for Q queries
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Find all divisors of a natural number | Set 2
- Find all divisors of a natural number | Set 1
- Largest proper fraction with sum of numerator and denominator equal to a given number
- Print all proper fractions with denominators less than equal to N
- Check if a number has an odd count of odd divisors and even count of even divisors
- Find all divisors of first N natural numbers
- Divisors of n-square that are not divisors of n
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Minimum value exceeding X whose count of divisors has different parity with count of divisors of X
- Find the Nth digit in the proper fraction of two numbers
- Sum of all prime divisors of all the numbers in range L-R
- Sum of all the prime divisors of a number
- Break a number such that sum of maximum divisors of all parts is minimum