Thread: Perfect Numbers

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    38

    Perfect Numbers

    Hey everyone hope all is well! Its that time of the week again and Im stuck already! lol I think I have gotten far this time, but still cant get things right.

    The project calls for a program to find all perfect numbers between 2 and 10,000. The program will determine whether a number is perfect by calling the function "isperfect". The function returns 1 if x is perfect and 0 if not. Print each perfect number on a seperate line along with its expansion as a sum of factors. The factors should appear in ascending order. For example the first line should read.

    6 = 1 + 2 + 3

    So I have managed to get the perfect number. Im not sure how to print out the sum of factors because thats not really how I wrote it. So where should I go from here?

    Code:
    #include<stdio.h>
    int isperfect(int x)
    {
        int total=0,i=0;
        for (i=0;i<(x/2);i++)
        {
            if (!(x%(i+1)))
            {
                total+=i+1;
            }
        }
        if (total==x) return 1;
        return 0;
    }
    
    int main(void)
    {
        int a=2,b=10000;
        for(; a<b; a++)
        if(isperfect(a))
        printf("%d TEST TEST\n",a,);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You run the code, and check your results.
    List of numbers - Wikipedia, the free encyclopedia
    Does it produce ALL the numbers in your range and no others?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your isperfect() function is finding the factors (it is adding them up). Store them somewhere (for example, in an array) as well so you can print them later.

    Alternatively, once main() has detected a perfect value, implement another loop to print out all the factors.

    And, no, I'm not going to write code to do either of those options. It is your homework, not mine.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Yes it does Salem but it only prints the number.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    ok grumpy nowhere in my question did I ask for anyone to write code for me. Just was stuck and wanting some advice, which I appreciate. I have not learned arrays yet so that suggestion is out the window. Let me try adding another loop to print the factors...

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Im not sure how to store the factors?...

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Ok well this is what I did and it yields the correct output I need but I know there is a better way. All I need is put what I want in the print f statement. So again if someone could help me find a better way to do this it would be greatly appreciated. Im not sure how to save each integer that it finds? Where in the code to store it and then how to get it to print with the addition sign? Anyways here is the code I have.

    Code:
    #include<stdio.h>
    int isperfect(int x)
    {
        int total=0,i=0;
        for (i=0;i<(x/2);i++)
        {
            if (!(x%(i+1)))
            {
                total+=i+1;
            }
        }
        if (total==x) return 1;
        return 0;
    }
    
    int main(void)
    {
        int a=2,b=10000, total;
        for(; a<b; a++)
        if(isperfect(a))
        if (a==6)
        printf("%d = 1 + 2 + 3\n",a);
        else if(a==28)
        printf("%d = 1 + 2 + 4 + 7 + 14\n",a);
        else if(a==496)
        printf("%d = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248\n",a);
        else if (a==8128)
        printf("%d = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064\n",a);
        return 0;
    }
    Here is my sample run results which is exactly what is required...
    6 = 1 + 2 + 3
    28 = 1 + 2 + 4 + 7 + 14
    496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
    8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I guess it would be something like
    Code:
    #include<stdio.h>
    int isperfect(int x, int *result, int *resultLen)
    {
        int total=0,i=0;
        for (i=0;i<(x/2);i++)
        {
            if (!(x%(i+1)))
            {
                result[resultLen++] = i;
                total+=i+1;
            }
        }
        if (total==x) return 1;
        return 0;
    }
    
    int main(void)
    {
        int a=2,b=10000, total;
        for(; a<b; a++) {
            int factors[10];
            int num = 0;
            if ( isperfect(a,factors,&num) ) {
                // print stuff in array
            }
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, there are better ways.

    The first way that does not involve hard coding the list is the "dumb" way. You basically reuse the loop from isperfect in your print function. This requires you run the loop twice, which can take a while on larger numbers.

    The second way, as grumpy suggested, is to pass an array to isperfect. Everytime isperfect finds a factor, you store it in the next slot in the array. You also need to pass in a number to keep track of how many factors there are, which will also help you track where the next free slot in the array is as you loop. Then, when your main loop finds a perfect number, you print the perfect number along with the list of factors stored in the array. This only requires calculating the factors once, and is, IMO, a much better way.

    Either way, putting the print code in it's own function would be a good idea.

    To get you started, here is the function signature for the second method and the call from main:
    Code:
    int isperfect(int x, int factors[], int *n_factors)
    ...
    int main(void)
    {
        int a, n_factors, factors[100];
    
        for (a = 2; a < 10000; a++)
            if (isperfect(a, factors, &n_factors))
    }
    EDIT: Looks like Salem beat me to it. There's also a cool pattern to the list of factors if you can figure it out. It actually would allow you to generate the list instead of testing every possible number and all it's factors.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Hi salem thanks so much for the response! However, we have not covered arrays yet so I cannot use them. Even though from your comments and also grumpys it looks like that would be the proper way to code this. Any other suggestions?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Copy/paste your code from the function, and just print the answers instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Thanks anduril for the tip. Can you explain further the "dumb" way you suggested? We dont learn about arrays until next week so I dont want to write the code using an array. How would I reuse the loop for isperfect to display the factors in my printf statement?

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Quote Originally Posted by Salem View Post
    Copy/paste your code from the function, and just print the answers instead.
    Ok so paste it in my main () after It finds "if isperfect(a)"? and then add a printf statement to print the total? Im not quite understanding. I know you say just cut and paste but its not so simple for me lol..thanks!

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, I'm trying to not give away the farm here. If you actually understand what the loop in isperfect does, then you should be able to fill in print_perfect quite easily.

    Code:
    int isperfect(int x)
    {
        int total=0,i=0;
        for (i=0;i<(x/2);i++)
        {
            if (!(x%(i+1)))
            {
                total+=i+1;
            }
        }
        if (total==x) return 1;
        return 0;
    }
    
    
    void print_perfect(int x)
    {
        // similar loop to that in isperfect, but instead of adding to total
        // you need to print every factor you find, with a + between them
    }
    
    
    int main(void)
    {
        int a=2,b=10000;
        for(; a<b; a++)
            if(isperfect(a))
                print_perfect(a);
        return 0;
    }

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Perfect Numbers
    By iLike in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2009, 12:27 PM
  2. perfect numbers
    By budala in forum C Programming
    Replies: 3
    Last Post: 08-08-2009, 04:16 PM
  3. perfect numbers
    By tomahawker in forum C++ Programming
    Replies: 14
    Last Post: 12-11-2005, 03:50 PM
  4. Perfect Numbers
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 09-06-2002, 02:48 PM
  5. Perfect Numbers - Need Help
    By cstraw in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2001, 05:28 PM