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