# Aliquot sum

In number theory, the **aliquot sum** s(n) of a positive integer n is the sum of all proper divisors of n, that is, all divisors of n other than n itself.

They are defined by the sums of their aliquot divisors. The aliquot divisors of a number are all of its divisors except the number itself. The aliquot sum is the sum of the aliquot divisors so, for example, the aliquot divisors of 12 are 1, 2, 3, 4, and 6 and it’s aliquot sum is 16.

A number whose **aliquot sum** equals its value is a PERFECT number (6 for example).

**Examples :**

Input : 12 Output : 16 Explanation : Proper divisors of 12 is = 1, 2, 3, 4, 6 and sum 1 + 2 + 3 + 4 + 6 = 16 Input : 15 Output : 9 Explanation : Proper divisors of 15 is 1, 3, 5 and sum 1 + 3 + 5 = 9

A **simple solution **is to traverse through all numbers smaller than n. For every number i, check if i divides n. If yes, we add it to result.

## C++

`// CPP program for aliquot sum ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to calculate sum of ` `// all proper divisors ` `int` `aliquotSum(` `int` `n) ` `{ ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `if` `(n % i == 0) ` ` ` `sum += i; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 12; ` ` ` `cout << aliquotSum(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for aliquot sum ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate sum of ` ` ` `// all proper divisors ` ` ` `static` `int` `aliquotSum(` `int` `n) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) ` ` ` `if` `(n % i == ` `0` `) ` ` ` `sum += i; ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `throws` `IOException ` ` ` `{ ` ` ` `int` `n = ` `12` `; ` ` ` `System.out.println(aliquotSum(n)); ` ` ` `} ` `} ` ` ` `/* This code is contributed by Nikita Tiwari.*/` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program for aliquot sum ` ` ` `# Function to calculate sum of ` `# all proper divisors ` `def` `aliquotSum(n) : ` ` ` `sm ` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `,n) : ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `) : ` ` ` `sm ` `=` `sm ` `+` `i ` ` ` ` ` `return` `sm ` `# return sum ` ` ` ` ` `# Driver Code ` `n ` `=` `12` `print` `(aliquotSum(n)) ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program for aliquot sum ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate sum of ` ` ` `// all proper divisors ` ` ` `static` `int` `aliquotSum(` `int` `n) ` ` ` `{ ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `if` `(n % i == 0) ` ` ` `sum += i; ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` ` ` `{ ` ` ` `int` `n = 12; ` ` ` `Console.WriteLine(aliquotSum(n)); ` ` ` `} ` `} ` ` ` `/* This code is contributed by vt_m.*/` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for aliquot sum ` ` ` `// Function to calculate sum of ` `// all proper divisors ` `function` `aliquotSum(` `$n` `) ` `{ ` ` ` `$sum` `= 0; ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `if` `(` `$n` `% ` `$i` `== 0) ` ` ` `$sum` `+= ` `$i` `; ` ` ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver Code ` `$n` `= 12; ` `echo` `(aliquotSum(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

16

**Efficient Solutions : **

Sum of all proper divisors of a natural number

Sum of all the factors of a number

## Recommended Posts:

- Aliquot Sequence
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Print all the permutation of length L using the elements of an array | Iterative
- Sum of N terms in the expansion of Arcsin(x)
- Minimize the cost of buying the Objects
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Right most non-zero digit in multiplication of array elements
- Find the remainder when First digit of a number is divided by its Last digit
- Find the remaining balance after the transaction
- Count of integers that divide all the elements of the given array
- Count number of ways to get Odd Sum
- Percentage increase in the volume of cuboid if length, breadth and height are increased by fixed percentages
- Count the number of occurrences of a particular digit in a number
- Find number of factors of N when location of its two factors whose product is N is given

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.