# Thread: Perfect Number Problem

1. how come is using void for main() a bad thing?
Skippers post adresses this very well:

'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!"

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. )

2. > how come is using void for main() a bad thing? just so I'll know whenever I come into a potential problem
http://users.aber.ac.uk/auj/voidmain.shtml
Courtesy of google

3. To find if a number is Perfect, really fast:

For values of n starting at 2 and going up, try:

(2^n - 1) This will give you the value of 3.

You can check if a number is prime with this function I wrote:

Code:
```bool checkIfPrime(int num){
int factor = num / 2; //begin from halfway, work way down to 1 (since 2 * (num / 2) = (num / 2) * 2)

while (factor > 1){ //until you are greater than 1. 1 is not prime; if you get this far, num is prime

if (num % factor == 0) //something other than 1 and num fit into num
return(false); //return false, num is NOT prime

factor--; //check next lowest value to see if it fits into num

}

return(true); //left loop after factor equaled 1, num is prime, return true

} //end bool checkIfPrime```
If the number is not prime, increase n and try the (2^n - 1) again and recheck if it's prime.

IF IT IS PRIME... which 3 is... multiply the answer you got from that first equation (3) by (2 ^ (n - 1)). (2 ^ (n - 1)) will equal 2. 2 times 3 is... 6. A perfect number.

__________________________________________________ _

Algorithm:

1. Start variable n at 2.

2. Get the result from 2^n - 1 (code for this is (pow(2, n) - 1).

3. The RESULT of this equation is checked if it's prime using the function above. For this, just make a bool variable like IsPrime and call isPrime = checkIfPrime(num);

4. If the number is not prime, increase n and go back to step two.

5. If the number is prime, multiply (2^n - 1) by (2 ^ (n - 1)). The code looks like (pow(2, n) - 1) * (pow(2, n - 1)). This will result in a perfect number.

6. Repeat these steps as much as you want using a loop.

7. Note of warning: These numbers get really big, really fast. You should also check for overflow error *just check if the perfect number you get is negative. If it is, output "OVERFLOW ERROR"*

Hope this helps

__________________________________________________ __

4. int factor = num / 2; //begin from halfway, work way down to 1 (since 2 * (num / 2) = (num / 2) * 2)

You could also just get the square root here, it may be a bit faster.

int factor = sqrt(num);

5. So what would the code be if you wanted say, all the prime numbers before the prog crashed, not just the first 5?

6. Do the loop while the perfect number variable is not negative

7. Why do my user names keep switching betwen Breek and Nakeerb? Bleh.

Popular pages Recent additions