Problem finding perfect numbers above 8128

Hi all, I'm writing a program to identify perfect numbers from 1 to 'n'. My code works fine in finding numbers up to 8128, but for some reason I can't find the next perfect number (33550336, as listed in Wikipedia). I suspect I'm reaching some size limit with my variables, but I've experimented with using long ints, which on my machine should extend to a max value of 2 147 483 647, and that didn't solve the problem. Any ideas? Code follows...

Just so you guys know, I am not a programming student, and this is not for a class; otherwise I'd ask for help from the professor or another student. I'm just trying to learn C++ for fun using a Deitel book from 2003 I got from the library. Unfortunately, I'm just starting out, and when I hit a snag like this, I'm not sure where to get help. Thanks in advance

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

bool perfect( long );

int main()
{
cout << setw(12) << "Integer" << endl;
cout << setw(12) << "*******" << endl;

for( long i = 1; i <= 34000000; i++ ) {
if (perfect(i))
cout << setw(12) << i << endl;
}

return(0);
}

bool perfect( long number )
{
long sum = 0;

for ( long j = 1; j < number / 2; j++) {
if ( number % j == 0 )
sum += j;
}

if ( sum == number )
return(true);
else
return(false);
}
Substituting your line:

for ( long j = 1; j < number / 2; j++) {

with:

for ( long j = 1; j <= number / 2; j++) {

yields the first four correct perfect numbers (I didn't wait to see if it was correct beyond that).
So, originally, I had "j < number", but after reviewing some code online for finding perfect numbers, i realized that i could make the shortcut of dividing the number by 2, I had just forgotten to put in the <= sign.

That being said, I'm still hitting a limit after finding the first four perfect numbers, which is the same problem I've had with my code from the beginning. Could it be that my program just freezes up and stops working? Or is there some inherent limit that I'm hitting that's keeping me from finding the fifth perfect number? I'm using Microsoft Visual as my IDE. Honestly, I don't know enough about the software to know what's going on with the program as it's running.

Any thoughts?
I can't see any reason that your program shouldn't get to the fifth perfect number. It may be that, since P5 is much, MUCH bigger than P4, it takes a long time to get there. If you think your program may be freezing (which I doubt), you could put telltales in, such as printing a line after every 100,000 efforts or so.
Another observation: you're using a rather brute force method of looking for the perfect numbers. You might consider using Euler's observation that only the expression 2p−1(2p−1) (where p is a Mersenne prime) will yield a perfect number. That would speed up your calculations considerably.

For more information on this:

http://en.wikipedia.org/wiki/Perfect_number
Last edited on
I did see Euler's observation/shortcut (though I think it's limited to only finding even perfect numbers, which still leaves open the question of whether an odd perfect number is possible--I think...), but I'm not concerned so much with finding perfect numbers for it's own sake so much as I am interested in the programming side of things and why the program isn't working as I think it should be.

I just tried inserting a cout line every 1000th number tested, and I've found the problem. My program is running, but just running very slowly as it gets up that high. Testing at 200,000 for perfection, the program is going through about 2000 tests/second. A minute or so later, testing number 300,000, the program was only doing about 1300 tests/second. So, the program was still running, but it would have taken way too long to get up to the fifth perfect number. After seeing the first four pop up almost instantaneously, I just assumed my computer could handle getting to the fifth, but it's taking a lot longer than I at first imagined it would.

Thanks for the quick reply on this mzimmers, I appreciate the help
Last edited on
Topic archived. No new replies allowed.