Thread: Perfect Number Problem

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Lightbulb 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
    Last edited by TrazPFloyd; 10-19-2002 at 01:27 AM.

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Can you describe what a perfect number is?
    I had this exercise once, but I can't remember it.

  3. #3
    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

  4. #4
    oh and you didnt return 0; to end main

    woops, u voided main, sooo nevermind. no sleep=bad times
    Last edited by Cgawd; 10-19-2002 at 05:18 AM.

  5. #5
    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 :-/

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    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.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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!"

    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.
    Last edited by skipper; 10-19-2002 at 11:21 AM.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  9. #9
    imhungry
    Guest

    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. #10
    imhungry
    Guest

    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;
    }

  11. #11
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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...

    calculating the 5th number is very computer-resource-straining. however, there is a pattern linking perfect numbers, i think. check google for more information.

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    Shadow12345
    Guest
    My math teacher gave me an entire book dedicated to perfect numbers, and yes there is some pattern linking all of them.

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    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

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    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

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, 05:00 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM