Find the Perfect Number

First of all, we have the conclusion from Euclid that define an integer n, and if 2^n – 1 is a prime, the number 2^(n-1)*(2^n – 1) must be a Perfect Number.

Here follows my source code on finding 5 the smallest Perfect Number.It’s written by C++.

using namespace std;
bool IsPrime (long long s)
{
	for(long long i=2;i<sqrt((long double)s);i++)
	{
		if(s%i==0) return false;
	}
	return true;
}
long long Power(int n)
{
	if(n!=0) return 2*Power(n-1);
	else return 1;
}
int main()
{
	int cnt=0;
	for(int i=2;cnt<9;i++)
	{
		if(IsPrime(Power(i)-1)) cout<<++cnt<<") "<<power(i-1)*(Power(i)-1)<<endl;
	}
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.