【Description】
Find the perfect number between positive integers 2 and n (one number per row).
Perfect number: the sum of the factors is equal to its own natural number, such as 6=1+2+3
【enter】
Enter n (n≤5000) .
【Output】
One number per line, in ascending order.
【Input example】
7
【Example of output】
6
C++:
#include<iostream>
using namespace std;
int judge(int a);
int main()
{
int n,k;
cin>>n;
for(k=2;k<=n;k++)
if(judge(k)==k) //判断该自然数的因子之和是否等于本身
cout<<k<<endl;
return 0;
}
int judge(int a) //该函数用来计算自然数的因子之和
{
int i;
int term=0;
for(i=1;i<=a-1;i++)
if(a%i==0)
term+=i;
return term;
}
C language:
#include<stdio.h>
int judge(int a);
int main()
{
int n,k;
scanf("%d",&n);
for(k=2;k<=n;k++)
if(judge(k)==k) //判断该自然数的因子之和是否等于本身
printf("%d\n",k);
return 0;
}
int judge(int a) //该函数用来计算自然数的因子之和
{
int i;
int term=0;
for(i=1;i<=a-1;i++)
if(a%i==0)
term+=i;
return term;
}