功能
用到的觀念
CODE
#include<iostream>//標頭檔
#include<iomanip>
#include<cmath>
using namespace std;
using std::setw;
using std::fixed;
using std::uppercase;
using std::setprecision;
void PerfectNumber(register unsigned long int num,unsigned long int
&);//declare the prototype
int main()
{
register unsigned long int a,i,j,k,co=0;//declare variable
cout<<"enter the number n and it'll find the perfect number from 1 to
n:"<<endl;//顯示內容
cin>>a;//appoint the end number to a
cout<<"by the way it can find 33550336 in almost 1 hour"<<endl;
for(i=2;i<=a;i++)//run the loop
{//co is the counter
PerfectNumber(i,co);//call the function. i is the number we
want to check
}
cout<<"the total is "<<co<<endl;//show the information
system("pause");
return 0;
}
void PerfectNumber(register unsigned long int num,unsigned long int &count)
{
register unsigned long int i,j,sum=0;//declare variable
if(num%10==6||num%10==8&&num<10000)//if the number is odd it's not a
perfect number
{
for(j=1;j<=sqrt((double)num);j++)//run the loop
if(num%j==0)//use mod
{
sum=sum+j+num/j;//add the factor to sum
}
sum=sum-num;
if(sum==num)//check if the total of the factor is
equal to the number
{
cout<<num<<" is a perfect
number"<<endl;//show the information
count=count+1;//count the number
}
}
else if(num%10==6||num%10==8&&num>10000)//if the number is odd it's
not a perfect number
{
for(j=1;j<=(sqrt((double)num)-1);j++)//run the loop
if(num%j==0)//use mod
{
sum=sum+j+num/j;//add the factor to sum
}
sum=sum-num;
if(sum==num)//check if the total of the factor is
equal to the number
{
cout<<num<<" is a perfect
number"<<endl;//show the information
count=count+1;//count the number
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.7.59