Perfect Number Problem

This is a discussion on Perfect Number Problem within the C++ Programming forums, part of the General Programming Boards category; how come is using void for main() a bad thing? Skippers post adresses this very well: 'void main()' causes "undefined" ...

  1. #16
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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. #17
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > 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
    The world is waiting. I must leave you now.

  3. #18
    Registered User
    Join Date
    Jun 2002
    Posts
    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. #19
    Registered User
    Join Date
    Jun 2002
    Posts
    3
    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. #20
    Banned Cgawd's Avatar
    Join Date
    Oct 2002
    Posts
    405
    So what would the code be if you wanted say, all the prime numbers before the prog crashed, not just the first 5?

  6. #21
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Do the loop while the perfect number variable is not negative

  7. #22
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Why do my user names keep switching betwen Breek and Nakeerb? Bleh.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  2. strange number problem
    By kkjj in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 07:30 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21