Thread: Pythagorean Triplet Problem

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    46

    Pythagorean Triplet Problem

    I am solving the euler probelm (no. 9) that wants to find the only Pythagorean triplet such that a+b+c = 1000. Does anyone see what is wrong with my code? It looks like it should work to me...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int a,b,c;
        int max = 400;
        int aSquared = a*a;
        int bSquared = b*b;
        int cSquared;
        
        for(a = 10; a < max; a++)
        {
              for(b = 10; b < max; b++)
              {
                    cSquared = aSquared + bSquared;
                    c = sqrt(cSquared);
                    if(a + b + c == 1000)
                        printf("%d is a, %d is b, %d is c.\n", a, b, c);
              }
        }
        
        system("PAUSE");
        return 0;
    }

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    You need to have these two lines of code inside the inner for loop, before the 'cSquared = aSquared + bSquared;' line:
    Code:
    aSquared = a*a;
    bSquared = b*b;

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    c = sqrt(cSquared);
    Based on a recent post you might have rounding errors on sqrt; not sure if you will have the error, since you were using integer sqrt; and, the orther person was using double sqrt.
    Code:
    c = (int)(sqrt((double)cSquared) + 0.5);

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    Thanks, I can't believe I didn't put that inside the loops. Anyways, it's still coming up with the wrong answer. It comes up with a long list of answers that don't fit the Pythagorean theorem. Like a^2 + b^2 != c^2....Any ideas as too what I am doing wrong here?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int a,b,c;
        int max = 500;
        int aSquared = pow(a,2);
        int bSquared = pow(b,2);
        int cSquared;
        
        for(a = 1; a < max; a++)
        {
              for(b = 1; b < max; b++)
              {
                    int aSquared = a*a;
                    int bSquared = b*b;
                    cSquared = aSquared + bSquared;
                    c = sqrt(cSquared);
                    if(a<b && b<c && a + b + c == 1000)
                        printf("%d is a, %d is b, %d is c.\n", a, b, c);
              }
        }
        
        system("PAUSE");
        return 0;
    }

  5. #5
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    There is no need to redefine 'aSquared' and 'bSquared' as integers within the loop. You already declared them as variables.

    The problem you are experiencing is probably caused by using integer types to represent the output from a square root function. You really should be using doubles for all your squared numbers (not just cSquared).

    This probably isn't causing any problems with the running of the program, but it might be a good idea to put brackets around some of the contents of your if statement:
    Code:
    if( (a<b)&&(a + b + c == 1000) )
    I don't know why you check that b < c in your if statement, because with the exception of a = 0, this will always be true, but since your loops start at 1 the a = 0 case isn't even considered. You might as well write:
    Code:
    if( 1&&( /*Some other crap */ )

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is there some restriction from using the obvious three nested loops for this problem?

    Not a particularly elegant solution, but it finds the answer in less than one second.

    Code:
    #include <stdio.h>
    
    #define MAX 500
    
    int main()
    {
      unsigned long int a,b,c,asqr,bsqr,csqr;
      for(a=1;a<MAX;a++) {
        for(b=1;b<MAX;b++) {
          for(c=1;c<MAX;c++) {
            if(a + b + c == 1000) {
              asqr=a*a;
              bsqr=b*b;
              csqr=c*c;
              if(asqr + bsqr == csqr) {
                printf("\nasqr=%2lu  bsqr=%2lu  csqr=%2lu\n", asqr, bsqr, csqr);
                printf("\nAnswer is: a=%lu b=%lu c=%lu", a,b,c);
                printf("\n\n\t\t\t    press enter when ready");
                (void) getchar();
                return 0;
              }
            }
          }
        }
      }
      printf("\n\n\t\t\t    press enter when ready");
      (void) getchar();
      return 0;
    }
    Last edited by Adak; 11-02-2010 at 01:31 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your approach is pretty good Adak, in that it illustrates the value of doing things in a slightly different order to avoid unnecessary computations - your code only adds squares of values if you've found a triplet that adds to 1000.

    I wouldn't actually use the innermost loop (over c) though ..... instead compute c = 1000 - a - b. That addresses your inelegance concern too ....

    The only other thing to watch is potential for overflow - squaring integer values and adding them has potential for overflow. Admittedly, it's not much of a problem in this particular case (MAX is defined as a small value compared with range of an unsigned long) but it is something to check if the value of MAX is redefined to something larger.
    Last edited by grumpy; 11-02-2010 at 02:17 AM.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Problem with Pythagorean triple etc.
    By -Prime- in forum C Programming
    Replies: 10
    Last Post: 10-14-2006, 01:50 AM