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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式語言
C++
C#
MongoDB
MySQL
Javascript
PHP