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 divisorsint aliquotSum(int n){Â Â Â Â int sum = 0;Â Â Â Â for (int i = 1; i < n; i++) Â Â Â Â Â Â Â Â if (n % i == 0) Â Â Â Â Â Â Â Â Â Â Â Â sum += i;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return sum; }Â
// Driver Codeint main(){Â Â Â Â int n = 12;Â Â Â Â cout << aliquotSum(n); Â Â Â Â return 0;} |
Java
// Java program for aliquot sumimport 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.*/ |
Python3
# Python 3 program for aliquot sumÂ
# Function to calculate sum of # all proper divisorsdef aliquotSum(n) :    sm = 0    for i in range(1,n) :        if (n % i == 0) :            sm = sm + i             return sm # return sumÂ
Â
# Driver Coden = 12print(aliquotSum(n))Â
# This code is contributed by Nikita Tiwari. |
C#
// C# program for aliquot sumusing 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.*/ |
PHP
<?php// PHP program for aliquot sumÂ
// Function to calculate sum of // all proper divisorsfunction 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.?> |
Javascript
<script>Â
// JavaScript program for aliquot sumÂ
// Function to calculate sum of // all proper divisors    function aliquotSum(n)    {        let sum = 0;        for (let i = 1; i < n; i++)             if (n % i == 0)                 sum += i;                           return sum;     }       Â
// Driver code                 let n = 12;        document.write(aliquotSum(n));         </script> |
Output :Â
16
Time Complexity: O(n)Â
Auxiliary Space: O(1)
Efficient Solutions :Â
Sum of all proper divisors of a natural numberÂ
Sum of all the factors of a number
Â
Please suggest if someone has a better solution which is more efficient in terms of space and time.
This article is contributed by Aarti_Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Read More Info here to that Topic: geeksforgeeks.org/aliquot-sum/ […]
… [Trackback]
[…] Info on that Topic: geeksforgeeks.org/aliquot-sum/ […]
… [Trackback]
[…] Find More on that Topic: geeksforgeeks.org/aliquot-sum/ […]
… [Trackback]
[…] Info to that Topic: geeksforgeeks.org/aliquot-sum/ […]
… [Trackback]
[…] Info on that Topic: geeksforgeeks.org/aliquot-sum/ […]