Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C program-perfect

  1. Dec 10, 2007 #1
    I need to write a program to check whether a number is perfect
    eg: 6=1+2+3 , 28=1+2+4+7+14
    i wrote one, but it doesnt work. also ive seen programs written with different preprocessors, we are required to write it on stdio.h and math.h


    #include<stdio.h>
    #include<math.h>

    int main()
    {
    int a,b,c,d,i;
    scanf("%d",&a);
    {
    for(i=1;i<a;i++)
    b=a%i;
    if(b==0)
    {
    c=0;
    c=c+i;
    }
    if(c==a)
    printf("perfect\n");
    }
    return 1;
    }
    
     
  2. jcsd
  3. Dec 10, 2007 #2
    Few mistakes in variable initialization and placing of parentheses. Here is the modified correct version. Make sure you understand the logic.

    #include<stdio.h>
    #include<math.h>

    int main()
    {
    int a,b,c,d,i;
    scanf("%d",&a);c=0;
    for(i=1;i<a;i++)
    {
    b=a%i;
    if(b==0)
    {
    c=c+i;
    } }
    if(c==a)
    printf("perfect\n");
    return 1;
    }
     
  4. Dec 10, 2007 #3

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    Heh. Here's a version you can't turn in, but which is far faster; it uses the Euclid identity of perfect numbers.

    Code (Text):
    #include<stdio.h>
    #include<math.h>

    int is_prime(n) {
      int i,root;
      if (!(n&1))
        return n == 2;
      if (n%3 == 0)
        return n == 3;
      if (n < 25)
        return n > 1;
      root = (int)sqrt(n);
      for (i = 5; i <= root; i += 6)
        if (n%i == 0 || n%(i+2) == 0)
          return 0;
      return 1;
    }

    int main() {
      int even_part, odd_part;
      scanf("%d",&odd_part);
      even_part = 1;
      while (!(odd_part&3)) {
        odd_part = odd_part >> 2;
        even_part = even_part << 2;
      }
      if (!(odd_part&1)) {
        odd_part = odd_part >> 1;
        even_part = even_part << 1;
      }
      if (even_part << 1 == odd_part + 1 && is_prime(odd_part))
        printf("perfect\n");
      return 0;
    }
    Even faster and even cheatier, for 32-bit machines:

    Code (Text):

    int main() {
      int n;
      scanf("%d",&n);
      if (n == 6 || n == 28 || n == 496 || n == 8128 || n == 33550336)
        printf("perfect\n");
      return 0;
    }
     
    Last edited: Dec 10, 2007
  5. Dec 10, 2007 #4
    Thank you very much arunbg. Im not good at C programming.
    I do now understand the placing of brackets and c=0.
    If no bother can you explain what is the difference between return 1 and return 0
    Ive used them but with no real understanding
    Thanks again
     
  6. Dec 10, 2007 #5
    Lol...nice one.
     
  7. Dec 10, 2007 #6

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    Don't worry about it. These are error codes that the program returns, but they won't display or do anything unless they're checked by an outside program. 0 is supposed to mean "terminated normally" and anything other than 0 is supposed to mean "terminated with some kind of error".
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C program-perfect
  1. C program (Replies: 4)

  2. C Program (Replies: 3)

  3. C program (Replies: 0)

  4. C programming (Replies: 9)

Loading...