Aliquot Sequence
Last Updated :
11 Sep, 2023
Given a number n, the task is to print its Aliquot Sequence. Aliquot Sequence of a number starts with itself, remaining terms of the sequence are sum of proper divisors of immediate previous term. For example, Aliquot Sequence for 10 is 10, 8, 7, 1, 0. The sequence may repeat. For example, for 6, we have an infinite sequence of all 6s. In such cases we print the repeating number and stop. Examples:
Input: n = 10
Output: 10 8 7 1 0
Sum of proper divisors of 10 is 5 + 2 + 1 = 8.
Sum of proper divisors of 8 is 4 + 2 + 1 = 7.
Sum of proper divisors of 7 is 1
Sum of proper divisors of 1 is 0
Note that there is no proper divisor of 1.
Input : n = 6
Output : 6
Repeats with 6
Input : n = 12
Output : 12 16 15 9 4 3 1 0
Important Points:
- Numbers which have repeating Aliquot sequence of length 1 are called Perfect Numbers. For example 6, sum of its proper divisors is 6.
- Numbers which have repeating Aliquot sequence of length 2 are called Amicable numbers. For example 220 is a Amicable Number.
- Numbers which have repeating Aliquot sequence of length 3 are called sociable number.
- It is conjectured that every aliquot sequence ends in one of the following ways
- with a prime number which in turn ends with 1 and then 0.
- a perfect number
- a set of amicable or sociable numbers.
The solution mainly lies in the calculation of sum of all the proper divisors of the previous term.
- If we observe carefully, the divisors of the number n are present in pairs. For example if n = 100, then all the pairs of divisors are: (1,100), (2,50), (4,25), (5,20), (10,10)
- Using this fact efficiently compute divisors. While checking divisors we will have to be careful if there are two equal divisors as in case of (10, 10).
- In such case we will take only one of them in calculation of sum. This sum will contain sum of all the possible divisors so we have to subtract the number n from the sum of all divisors to get the sum of proper divisors.
We can generate the sequence by first printing the number n and then calculating the next terms using sum of proper divisors. When we compute next term, we check if we have already seen this term or not. If the term appears again, we have repeating sequence. We print the same and break the loop.
C++
#include <bits/stdc++.h>
using namespace std;
int getSum( int n)
{
int sum = 0;
for ( int i=1; i<= sqrt (n); i++)
{
if (n%i==0)
{
if (n/i == i)
sum = sum + i;
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum - n;
}
void printAliquot( int n)
{
printf ( "%d " , n);
unordered_set< int > s;
s.insert(n);
int next = 0;
while (n > 0)
{
n = getSum(n);
if (s.find(n) != s.end())
{
cout << "\nRepeats with " << n;
break ;
}
cout << n << " " ;
s.insert(n);
}
}
int main()
{
printAliquot(12);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int getSum( int n)
{
int sum = 0 ;
for ( int i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (n / i == i)
{
sum = sum + i;
}
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum - n;
}
static void printAliquot( int n)
{
System.out.printf( "%d " , n);
TreeSet<Integer> s = new TreeSet<>();
s.add(n);
int next = 0 ;
while (n > 0 )
{
n = getSum(n);
if (s.contains(n) && n != s.last())
{
System.out.print( "\nRepeats with " + n);
break ;
}
System.out.print(n + " " );
s.add(n);
}
}
public static void main(String[] args)
{
printAliquot( 12 );
}
}
|
Python3
from math import sqrt
def getSum(n):
summ = 0
for i in range ( 1 , int (sqrt(n)) + 1 ):
if n % i = = 0 :
if n / / i = = i:
summ + = i
else :
summ + = i
summ + = n / / i
return summ - n
def printAliquot(n):
print (n, end = " " )
s = set ()
s.add(n)
nextt = 0
while n > 0 :
n = getSum(n)
if n in s:
print ( "Repeats with" , n)
break
print (n, end = " " )
s.add(n)
if __name__ = = "__main__" :
printAliquot( 12 )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int getSum( int n)
{
int sum = 0;
for ( int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
{
sum = sum + i;
}
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum - n;
}
static void printAliquot( int n)
{
Console.Write(n+ " " );
HashSet< int > s = new HashSet< int >();
s.Add(n);
while (n > 0)
{
n = getSum(n);
if (s.Contains(n))
{
Console.Write( "\nRepeats with " + n);
break ;
}
Console.Write(n + " " );
s.Add(n);
}
}
public static void Main(String[] args)
{
printAliquot(12);
}
}
|
Javascript
function getSum(n){
let sum = 0;
for (let i=1; i<= Math.sqrt(n); i++){
if (n%i==0){
if (n/i == i){
sum = sum + i;
}
else {
sum = sum + i;
sum = sum + Math.floor(n / i);
}
}
}
return sum - n;
}
function printAliquot(n){
const temp = new Array();
temp.push(n);
const s = new Set();
s.add(n);
let next = 0;
while (n > 0){
n = getSum(n);
if (s.has(n)){
console.log( "Repeats with " );
break ;
}
temp.push(n);
s.add(n);
}
const ans = temp.join( ' ' );
console.log(ans);
}
{
printAliquot(12);
return 0;
}
|
Output:
12 16 15 9 4 3 1 0
Reference: https://en.wikipedia.org/wiki/Aliquot_sequence
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...