Open In App

Aliquot Sequence

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:

The solution mainly lies in the calculation of sum of all the proper divisors of the previous term.

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++ implementation of Optimized approach
// to generate Aliquot Sequence
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate sum of all proper divisors
int getSum(int n)
{
    int sum = 0;  // 1 is a proper divisor
  
    // Note that this loop runs till square root
    // of n
    for (int i=1; i<=sqrt(n); i++)
    {
        if (n%i==0)
        {
            // If divisors are equal, take only one
            // of them
            if (n/i == i)
                sum = sum + i;
  
            else // Otherwise take both
            {
                sum = sum + i;
                sum = sum + (n / i);
            }
        }
    }
  
    // calculate sum of all proper divisors only
    return sum - n;
}
  
// Function to print Aliquot Sequence for an input n.
void printAliquot(int n)
{
    // Print the first term
    printf("%d ", n);
    unordered_set<int>  s;
    s.insert(n);
  
    int next = 0;
    while (n > 0)
    {
        // Calculate next term from previous term
        n = getSum(n);
  
        if (s.find(n) != s.end())
        {
            cout << "\nRepeats with " << n;
            break;
        }
  
        // Print next term
        cout << n << " ";
        s.insert(n);
    }
}
  
/* Driver program to test above function */
int main()
{
    printAliquot(12);
    return 0;
}




// Java implementation of Optimized approach 
// to generate Aliquot Sequence 
import java.util.*;
  
class GFG 
{
  
    // Function to calculate sum 
    // of all proper divisors 
    static int getSum(int n) 
    {
        int sum = 0; // 1 is a proper divisor 
  
        // Note that this loop runs till  
        // square root of n 
        for (int i = 1; i <= Math.sqrt(n); i++)
        {
            if (n % i == 0
            {
                // If divisors are equal, take only one 
                // of them 
                if (n / i == i) 
                {
                    sum = sum + i;
                }
                else // Otherwise take both 
                {
                    sum = sum + i;
                    sum = sum + (n / i);
                }
            }
        }
  
        // calculate sum of all proper divisors only 
        return sum - n;
    }
  
    // Function to print Aliquot 
    // Sequence for an input n. 
    static void printAliquot(int n) 
    {
          
        // Print the first term 
        System.out.printf("%d ", n);
          
        TreeSet<Integer> s = new TreeSet<>();
        s.add(n);
  
        int next = 0;
        while (n > 0
        {
            // Calculate next term from previous term 
            n = getSum(n);
  
            if (s.contains(n) && n != s.last()) 
            {
                System.out.print("\nRepeats with " + n);
                break;
            }
  
            // Print next term 
            System.out.print(n + " ");
            s.add(n);
        }
    }
  
    /* Driver code */
    public static void main(String[] args) 
    {
        printAliquot(12);
    }
}
  
// This code is contributed by Rajput-JI




# Python implementation of Optimized approach
# to generate Aliquot Sequence
  
from math import sqrt
  
# Function to calculate sum of all proper divisors
def getSum(n):
    summ = 0 # 1 is a proper divisor
  
    # Note that this loop runs till square root
    # of n
    for i in range(1, int(sqrt(n)) + 1):
        if n % i == 0:
  
            # If divisors are equal, take only one
            # of them
            if n // i == i:
                summ += i
  
            # Otherwise take both
            else:
                summ += i
                summ += n // i
  
    # calculate sum of all proper divisors only
    return summ - n
  
# Function to print Aliquot Sequence for an input n.
def printAliquot(n):
  
    # Print the first term
    print(n, end=" ")
    s = set()
    s.add(n)
  
    nextt = 0
    while n > 0:
  
        # Calculate next term from previous term
        n = getSum(n)
  
        if n in s:
            print("Repeats with", n)
            break
  
        # Print next term
        print(n, end=" ")
        s.add(n)
  
# Driver Code
if __name__ == "__main__":
    printAliquot(12)
  
# This code is contributed by
# sanjeev2552




// C# implementation of Optimized approach 
// to generate Aliquot Sequence 
using System;
using System.Collections.Generic;
  
class GFG 
  
    // Function to calculate sum 
    // of all proper divisors 
    static int getSum(int n) 
    
        int sum = 0; // 1 is a proper divisor 
  
        // Note that this loop runs till 
        // square root of n 
        for (int i = 1; i <= Math.Sqrt(n); i++) 
        
            if (n % i == 0) 
            
                // If divisors are equal,  
                // take only one of them 
                if (n / i == i) 
                
                    sum = sum + i; 
                
                else // Otherwise take both 
                
                    sum = sum + i; 
                    sum = sum + (n / i); 
                
            
        
  
        // calculate sum of all proper divisors only 
        return sum - n; 
    
  
    // Function to print Aliquot 
    // Sequence for an input n. 
    static void printAliquot(int n) 
    
          
        // Print the first term 
        Console.Write(n+" "); 
          
        HashSet<int> s = new HashSet<int>(); 
        s.Add(n); 
  
        while (n > 0) 
        
              
            // Calculate next term from previous term 
            n = getSum(n); 
  
            if (s.Contains(n)) 
            
                Console.Write("\nRepeats with " + n); 
                break
            
  
            // Print next term 
            Console.Write(n + " "); 
            s.Add(n); 
        
    
  
    /* Driver code */
    public static void Main(String[] args) 
    
        printAliquot(12); 
    
  
/* This code has been contributed 
by PrinciRaj1992*/




// JavaScript implementation of Optimized approach
// to generate Aliquot Sequence
  
// Function to calculate sum of all proper divisors
function getSum(n){
    let sum = 0;  // 1 is a proper divisor
  
    // Note that this loop runs till square root
    // of n
    for (let i=1; i<= Math.sqrt(n); i++){
        if (n%i==0){
            // If divisors are equal, take only one
            // of them
            if (n/i == i){
                sum = sum + i;
            }
            else{
                // Otherwise take both
                sum = sum + i;
                sum = sum + Math.floor(n / i);
            }
        }
    }
  
    // calculate sum of all proper divisors only
    return sum - n;
}
  
// Function to print Aliquot Sequence for an input n.
function printAliquot(n){
    // Store the answer in temp array. 
    const temp = new Array();
    temp.push(n);
   
    // Creating a set. 
    const s = new Set();
    s.add(n);
      
    let next = 0;
    while (n > 0){
        // Calculate next term from previous term
        n = getSum(n);
  
        if (s.has(n)){
            console.log("Repeats with ");
            break;
        }
  
        // Print next term
        temp.push(n);
        s.add(n);
    }
    // Convert the given array to string 
    // and print it. 
    const ans = temp.join(' ');
    console.log(ans);
}
  
/* Driver program to test above function */
{
    printAliquot(12);
    return 0;
}
  
//The code is contributed by Gautam goel (gautamgoel962)

Output:

12 16 15 9 4 3 1 0 

Reference: https://en.wikipedia.org/wiki/Aliquot_sequence


Article Tags :