# Perfect Number Problem

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-19-2002
TrazPFloyd
Perfect Number Problem
Hello all,

I've got quite a task on my hands. I'm having to write a problem that will calculate and display the first five perfect numbers in C++ for a school assignment. This is a huge number crunching problem, as the perfect numbers are 6, 28, 496, 8126, and a huge 33550336. My program will find the fourth perfect number (8126) and then just doesnt do anything. Right now I'm letting the program go and wait and see if it will come up eventually. Here is my code along with program documentation explaining my mode of thought when writing this. All help is appreciated (this is quite long so bear with me):

Code:

```/*  The Perfect Numbers:         1. 6         2. 28     3. 496     4. 8128     5. 33550336 */         //        Include standard C++ header file #include <iostream.h> //        Begin program with main() statement of void type void main() {         //        Declare long integer objects x, divisor, I, and pcontrol         /*  Description (DESC) of each:             x will hold the number being tested as perfect number             divisor will hold the sum of the divisors of x             I will hold each divisible being tested                 and pcontrol will be the control object to keep the for loop going until 5 perfect numbers             are found */         long x, divisor, I, pcontrol;         //        Introduce user to program         cout << "**********************************************" << endl;         cout << "********* Perfect Number Generator ***********" << endl;         cout << "**********************************************" << endl << endl;         cout << "This program will generate the first five perfect numbers." << endl << endl;         //        Begin initial for statement to generate perfect numbers         /*  DESC:  x is set to 1, pcontrol is set to 1.  We want the for loop to continue         until pcontrol (representing the perfect numbers accounted for reaches five and then end         the loop */         for (x = 1, pcontrol = 1;  pcontrol <= 5;  x++)         {                 //        Begin nested for statement to compute sum of divisor                 /* DESC:  I is set to one, while the divisor is set to 0, this allows unique number                 testing after each termination of the loop to rejoin back at these values with the new number                 for x.  Continue doing this specific for loop until I <= x, and increment I after each iteration */                 for (I = 1, divisor = 0; I <= x; I++)                 {                         //        Begin an if statement to test if I divides x, if it does not, it will continue to next                         //        iteration.  If it does, add I to the sum of the divisors of x.                         if (x%I == 0)                         {                                 divisor += I;                         }                                                        // End if                 }                 // End for         //        Begin an if statement to test if the divisor is equal to 2 times x, and if it is         //        its a perfect number, else continue on with the loop and test the next x number         if (divisor == 2 * x)         {         cout << "The number " << x << " is a perfect number." << endl;         pcontrol++;         }         // End if                 }         // End for         cout << "Thank you for your patience." << endl;         cin.get(); } // End main```
Thank you very much for your assistance

I should also mention that use of for loops is required in this assignment as we are covering for loops this week
• 10-19-2002
ammar
Can you describe what a perfect number is?
I had this exercise once, but I can't remember it.
• 10-19-2002
Cgawd
i think its this line

long x, divisor, I, pcontrol;

maybe it should be float?
long double maybe?
or maybe you might want to make x something divisor something I another and pcontrol somethin else, look into that though
• 10-19-2002
Cgawd
oh and you didnt return 0; to end main

woops, u voided main, sooo nevermind. no sleep=bad times
• 10-19-2002
Cgawd
Quote:

I should also mention that use of for loops is required in this assignment as we are covering for loops this week
so i dont think that will solve his problem :-/
• 10-19-2002
skipper
main() is never, ever 'void'! It is always 'int'.

C++ returns '0' by default. (C does not, which is why 'return 0;' must be incorporated into C programs but may be ignored in C++ programs. Not a good habit, perhaps, but legal.) ;)

Also, Salem did, in fact, use FOR loops in his code.

Note how Salem reduces the amount of testing by half, i.e. once 'I' is greater than x/2, no number but 'x' itself will divide evenly into it. So simple, it's elegant. :)

P.S. ammar, you may have figured this out by now, but the idea is that the accumulated sum of the values that divide evenly into a given number must equal twice the number's value to be "perfect".

For example:

Accumulating 'divisor' for x = 28,

divisor = 1 + 2 + 4 + 7 + 14 + 28 = 56.

Therefore, divisor = 2 * 28. :)

-Skipper
• 10-19-2002
Zahl
I've always used int main instead of void main, but I really don't know why... just a habbit I gues. Aparently a good one ay.:D
• 10-19-2002
skipper
Zahl,

A very good habit! ;)

'void main()' causes "undefined" behavior. Now, what this seemingly innocent statement means is that your worst nightmare may have just started.

main() must return an integer value back to where it was called; not necessarily "the System".

A void function cannot return a value.

Borrowing from Mr. Yogi Berra, you're telling the compiler that when it comes to a fork in the road, it should take it. Say what???

In essence, that's what 'void main()' does. In the American vernacular, "That is, like, so not good!" :D

Check out Salem's avatar, btw. Pretty much says it all. (I'd mention Prelude, but her blood pressure goes too high when she sees 'void main()'. Far too young. :) )

-Skipper

P.S. By my very, very coarse calculations, it will take the compiler over 18.5 hours to iterate from 8128 to 33550336 on my machine. I'm thinking of upgrading from the Commodore VIC20 to the 64. :D
• 10-19-2002
imhungry
You might want to look at this
Here is the code for a program that asks the user for an integer and then prints all the perfect numbers that are less than it. So for example if the user put in 29, it would print 6 and 28. You will have to modify it to your needs but this is the core of the program.

#include <iostream.h>

int main()
{
int dont_close;
int number;
int N;
int sum=0;

cout << "Number: ";
cin >> N;

for (number=1; number<N; number++)
{
for (int i=1; i<number; i++)
{
if (number%i==0)
{
sum += i;
}
}
if (sum==number)
{
cout << sum << endl;

}

sum=0;

}

cin >> dont_close;
return 0;
}
• 10-19-2002
imhungry
You might want to take a look at this
Sorry, I forget to put the code tags, this should be a little easier to read.
Code:

```#include <iostream.h> int main() {         int dont_close;         int number;         int N;         int sum=0;         cout << "Number: ";         cin >> N;                 for (number=1; number<N; number++)         {                 for (int i=1; i<number; i++)                 {                         if (number%i==0)                         {                                        sum += i;                         }                        }                                if (sum==number)                         {                                 cout << sum << endl;                                                 }                                 sum=0;                                }                        cin >> dont_close;         return 0; }```
• 10-19-2002
ygfperson
perfect numbers are numbers where all its factors (besides itself) add up to itself.

6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248

and so on...

• 10-19-2002
Sang-drax
I couldn't resist to post the Omicron verison:

Code:

```@include Expansion num = 0 index = 2 repeat   factor = round (index/2)   Sum = 0   repeat     if factor|index       Sum += factor   until (factor-=1)==0   if Sum == index   begin     println "Perfect number:" + index     num += 1   end   index += 2 until num>=3 call wait```
• 10-19-2002
My math teacher gave me an entire book dedicated to perfect numbers, and yes there is some pattern linking all of them.
• 10-19-2002
TrazPFloyd
Hey guys,

Thanks a lot for your help... however it seems like no matter what code I use (I tried some of yours) its gonna take a really really REALLY long time to find that fifth perfect number. With that being said I believe that this is probably the best that can be done and I should probably go ahead and turn the code in.

Some of you were saying about how we should always use int main()... well my C++ professor recommends us using void main()

Don't know why

Thanks again
Travis
• 10-19-2002
TrazPFloyd
lol, i dont necessarily agree with him. I'm still very new to C++ so I've a lot to learn.

how come is using void for main() a bad thing? just so I'll know whenever I come into a potential problem

thanks
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last