so im not new to programming. but have been off the scene for a long while.
taking c++ in college at the moment and i recently got a request from a friend up in seattle, washington (me down in california) to make a program that computes all the perfect numbers from 0 to 10,000. simple enough. but i wanted to take it further, to try and compute as many perfect numbers as i can. according to wikipedia there are 44 recorded even perfect numbers. my program can compute the first seven. it works perfectly. the only problem i encounter is the digit range limitations of c++. the eighth perfect number is too large to handle. if i try to do anything bigger than what i have it stops running. is there anyway around this?

float prime_factor_check;//variable where value of ((2^n)-1) stored so it can be checked if it is a prime number
__int64 perfect_number = 0;//variable where the computed value of a valid perfect number is stored
int n = 0;//the 'n' value in the expression (pow(2.0,n-1) * (pow(2.0,n)-1))
bool prime = false;//boolean identifier to show if (pow(2.0,n)-1) is a prime number

void main()
{
cout << "The First Seven Perfect Numbers Are:" << endl << endl << ": ";

while(perfect_number < 100000000000)
{
primenumber();//call function primenumber()
perfectnumberoutput();//call function perfectnumberoutput()
n++;//increase 'n' once every program cycle
}

cout << endl << endl << "Hit any key to exit. . .";

while(_kbhit() == 0)
{//loop until a key is pressed
}
}

void primenumber()
{
for(int i = 2;i <= (pow(2.0,n)-1)-1;i++)
{//'for loop' to divide (pow(2.0,n)-1) by every number greater than 1 and less than itself
prime_factor_check = (pow(2.0,n)-1) / i;

if(!(prime_factor_check > static_cast<int>(prime_factor_check) && prime_factor_check < static_cast<int>(prime_factor_check + 1)))
{//'if statement' to check for any other factors of (pow(2.0,n)-1) besides 1 and itself making it a non prime number (composite number) and ending the 'for loop'
prime = false;
i = (pow(2.0,n)-1);
}
else
{//if there are no other factors of (pow(2.0,n)-1) besides 1 and itself then (pow(2.0,n)-1) is a prime number
prime = true;
}
}
}

void perfectnumberoutput()
{//outputs all of the perfect numbers that were computed
if(prime == true)
{
if(perfect_number < 10000000000)
{
perfect_number = (pow(2.0,n-1) * (pow(2.0,n)-1));
cout << fixed << setprecision(0) << perfect_number;
cout << " : ";
}
}
}

Edited by the Author.

Edited by the Author.

n/a

DaVince This fool just HAD to have a custom rating

Registered 04/09/2004
Points 7998

3rd November, 2007 at 07:23:33 -

Maybe it's your compiler?

http://www.thescripts.com/forum/thread63790.html
In that (2005) thread there are some people stating that __int64 is not (fully) supported in some compilers, or implemented in a different way.

i actually read that thread in my search for answers before i posted here.
Ron Natalie in that thread remarks "__int64 is a[n] implementation extension by some compilers (notably Visual C++)."

i'm using visual c++ 2005

__int64 can hold integers up to 19 digits and the integer 9223372036854775807 (i tested, it will hold and output that number from an __int64 variable) which should be perfectly fine for the 8th perfect number coming out to be 2305843008139952128, less than the maximum of __int64.

shouldnt there be another way perhaps around this?

im guessing no one really wants to help at this point or doesnt want to take the time to. but i do beleive i have figured out my problem for the eighth perfect number. the algorithm used to compute if (2^n)-1 is a prime number is just taking a long time. its a pretty big number to check. (2^31)-1 = 2147483647, which by my algorithm has to divide itself by every number greater than one and less than itself. whereas the seventh perfect number is only (2^19)-1 = 524287. which is vastly less than the eighth. so my program will generate the 8th perfect number given enough time. buuuut. it will then come into the problem previously mentioned when trying to compute the 9th perfect number, not enough storage for such a big number. if anyone can help it would be greatly appreciated.

knowledge is power!

Edited by the Author.

n/a

Peblo Custom ratings must be 50 characters or less

Registered 05/07/2002
Points 185

4th November, 2007 at 23:22:38 -

Some people don't know languages outside of MMF .

"Isn't it always amazing how we characterize a person's intelligence by how closely their thinking matches ours?"
~Belgarath

how sad. knowing even just the basics of other languages makes it so much easier to code in the already very simple user interface of clickteam products.

anyway. aside from my number size limitations. i optimized my code so it produces the first 8 perfect numbers within about less than a second. if only someone did know how to help me. i will continue my search elsewhere.

thanks a bunch.

n/a

DaVince This fool just HAD to have a custom rating

Registered 04/09/2004
Points 7998

5th November, 2007 at 14:45:09 -

I know the basics of C++. I just don't know any math or why that won't work.

well the math is correct. the program is perfect. its just the numbers get way too big to handle. so i cant get anything bigger than the eighth number. unless theres away around that limitation.