Program to find all "complete numbers" within 500. The so-called "complete number" is a number that is exactly equal to the sum of all its different factors (including 1), and the cubic sum of its digits is equal to the number itself. Example: 6 = 1 + 2 + 3
#include <iostream>
#include <cmath>
using namespace std;
int main () {
for (int i = 2; i <= 500; ++ i) {
int sum = 0;
for (int j = 1; j <= sqrt ((double) i); ++ j) {
if (0 == i% j) {
sum + = j;
int j2 = i / j;
if (j2! = j&&j2! = i)
sum + = j2;
}
}
if (sum == i) {
cout << i << endl;
}
}
}
What's the point? The algorithm of this program is the simplest and most straightforward: find out all the factors of each number, and then add them up to see if it is this number. If it is, it means it is a complete number. ——This is not the subject itself.
[code] 1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5 int i=1,j=1,sum=0;
6 for(i=1;i<=500;i++)
7 {
8 sum=0;
9 for(j=1;j<i;j++)
10 {
11 if((i%j)==0)
12 sum=sum+j; //Judge whether it is a complete number
13}
14 if(sum==i) //If it is the output result
15 {
16 cout<<endl<<i<<"is Wan Bei Shu!"<<endl;
17 for(j=1;j<i;j++)//output all divisors
18 {
19 if(i%j==0)
20 cout<<" "<<j<<" ";
twenty one }
twenty two }
twenty three }
24 cout<<endl;
25 return 0;
26}
[/code]