Aliquot Sequence in C++


Aliquot sequence is a special sequence of numbers. The sequence starts from the number itself and the next number of the sequence is the sum of the proper divisors of the previous terms.

Let’s take an example of the sequence to learn the concept better −

Input : 8
Output : 8 7 1 0
Explanation :
   Proper divisors of 8 are 4, 2, 1. The sum is 7
   Proper divisors of 7 are 1. The sum is 1
   Proper divisors of 1 are 0. The sum is 0

Perfect number is the number that has the aliquot sequence of length one. For example, 6 is a Perfect number.

Amicable number is a number that has aliquot sequence of length two. For example, 1 is an Amicable number.

Sociable number is a number that has aliquot sequence of length three. For example, 7 is a Sociable number.

To calculate the alique sequence from a number. We need to calculate the proper divisors of the term. To calculate this we will use the division algorithm.

Algorithm

Step 1: Initialise the number.
Step 2 : Find all the proper divisors of the number.
Step 3 : Calculate the sum of all proper divisors.
Step 4 : Print the sum and go to step one and initialise number with this sum.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int Sumfactorial(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 Aliquotsequence(int n){
   printf("%d ", n);
   unordered_set<int> s;
   s.insert(n);
   int next = 0;
   while (n > 0){
      n = Sumfactorial(n);
      if (s.find(n) != s.end()){
         cout << "\nRepeats with " << n;
         break;
      }
      cout << n << " ";
      s.insert(n);
   }
}
int main(){
   Aliquotsequence(45);
   return 0;
}

Output

45 33 15 9 4 3 1 0

Updated on: 16-Oct-2019

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements