Thread: perfect numbers

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    perfect numbers

    I want to find the first 5 perfect numbers.

    This is my code, but there is a problem. My logic is that I stay inside the loop until i have the fifth number (i.e. test == 4) but it simply hangs. If I change the condition from (test<4) to (test<3) I get the first 4 perfect numbers without a problem though... I start from number = 4 because 2 and 3 are prime.

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) 
    {
    	long  perfect[5];
    	long  test = -1, i;
    	long  number = 4, sum;
    	
    	while (test < 4)
    	{
    		sum = 0;
    		
    		for (i=1; i< number; i++)
    		{
    			if (broj % i == 0) sum += i;
    		}
    		
    		if (sum == number)
    		{
    			test++;
    			perfect[test] = number;
    		}
    		
    		number ++;
    	}
    	
    	printf("::::: THE PERFECT NUMBERS ARE : ");
    	for (i=0; i<5; i++)
    	{printf("\t%d", perfect[i]);}
    	
    	return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because you increment test before you use it.

    Really, you should start with test = 0 and then increment at the end of the loop.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    It's not hanging, it's doing something. The question is, how fast is it doing that something? For each number, you're looping about that many times (that is, you're looping 99 times for the number 100, 999 for 1000, 9999 for 10000, and so on). Those will take longer and longer to complete.

    The fifth perfect number is 33550336. By the time you get there you will have performed trillions of loops. Your program will not complete in any reasonable amount of time.

    On an unrelated note, %ld is used to print out longs, not %d.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    I realized later that that takes toooo long. I need a different algorithm. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. Perfect Number Problem
    By TrazPFloyd in forum C++ Programming
    Replies: 21
    Last Post: 10-20-2002, 11:09 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM