C++中的等分序列


等分序列是一種特殊的數字序列。該序列從該數字本身開始,而序列的下一個數字是前一項真因子的和。

我們以一個序列示例,以便更好地理解這個概念 -

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

完數是指等分序列長度為一的數字,例如,6 是一個完數。

相親數是指等分序列長度為二的數字。例如,1 是一個相親數。

社團數是指等分序列長度為三的數字。例如,7 是一個社團數。

要計算得出某個數字的等分序列,我們需要計算該項的真因子。而要進行此計算,我們需要使用除法演算法。

演算法

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.

示例

 線上演示

#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;
}

輸出

45 33 15 9 4 3 1 0

更新時間: 2019-10-16

266 人次瀏覽

推進你的職業

完成本課程獲得認證

開始
廣告
© . All rights reserved.