Thread: Can't think of how to continue this prog....

  1. #1
    Unregistered
    Guest

    Can't think of how to continue this prog....

    I can't figure out how to continue the prog....it is supposed to find all perfect numbers from 1 to 100 and i'm supposed to make it "efficient". Here is what i have so far...not much...but i'm stuck now...

    #include <stdio.h>

    main()
    {
    int findPerfect(int num);
    void displayPerfect(int num);
    printf("This program displays a list of all perfect numbers from 1 to 100\n\n");
    displayPerfect();
    }

    void displayPerfect(bool findPerfect())
    {
    int counter;
    for(counter=1;counter<=100;counter++);
    {
    if(findPerfect(counter))
    {
    return (printf("%d\n",counter));
    }
    }
    }

    bool findPerfect(int num)
    {

    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Think about what a perfect # is (any number squared). Then, display the squares of all number from 1 to 10 because 10^2=100.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    Lightbulb Re: Can't think of how to continue this prog....

    Listen man I think you don't know what a perfect is!!!
    6 is the first != 1 because 2*3=6 %)).
    ....
    But in long type they are a few (three or four)!!!

    So this is a geeky way to find them but if you are obstinate
    you'll reach success. (I've just written the code in some seconds.)

    #include <stdio.h>

    long find_next_perfect(long k) {
    long i,sum=1,numb=k+1;
    int ok=0;

    while (!ok) {
    for (i=2;i<=numb/2;i++) sum*=i;
    ok=sum==numb;
    sum=1; numb++;
    }
    return --numb;
    }

    int main() {
    long p=0;

    while (1) { /* forever uuuh yea */
    p=find_next_perfect(p);
    printf("%ld\n",p);
    }
    return 0;
    }

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by golfinguy4
    Think about what a perfect # is (any number squared). Then, display the squares of all number from 1 to 10 because 10^2=100.
    A perfect number is one which is equal to the sum of its divisors less than it
    e.g, 6= 1 + 2 + 3
    28 = 1 + 2 + 4 + 7 + 14

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it is supposed to find all perfect numbers from 1 to 100 and i'm supposed to make it "efficient".
    Well, since you asked for something efficient and we know that there are only two perfect numbers from 1 to 100, we can either hard code it or use a lookup table:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      printf ( "%d %d\n", 6, 28 );
      return 0;
    }
    Code:
    #include <stdio.h>
    
    static int perfect[] = { 6, 28, -1 };
    
    int main ( void )
    {
      int i;
      for ( i = 0; perfect[i] != -1; i++ )
        printf ( "%d ", perfect[i] );
      printf ( "\n" );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I love that solution. I really do.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  3. Continue and switch
    By camzio in forum C Programming
    Replies: 10
    Last Post: 10-04-2008, 08:31 AM
  4. switch - continue
    By DavidP in forum C Programming
    Replies: 2
    Last Post: 06-24-2004, 10:09 AM
  5. continue in while loops
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-04-2002, 10:58 PM