Find Pythagorean triplet for which a + b + c = 1000

后端 未结 16 2424
离开以前
离开以前 2020-12-24 13:13

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2

For example, 32 + 4

相关标签:
16条回答
  • 2020-12-24 14:01
    #include <math.h>
    #include <stdio.h>
    
    int main()
    {
        const int sum = 1000;
        int a;
        for (a = 1; a <= sum/3; a++)
        {
            int b;
            for (b = a + 1; b <= sum/2; b++)
            {
                int c = sum - a - b;
                if ( a*a + b*b == c*c )
                   printf("a=%d, b=%d, c=%d\n",a,b,c);
            }
        }
        return 0;
    }
    

    explanation:

    • b = a;
      if a, b (a <= b) and c are the Pythagorean triplet,
      then b, a (b >= a) and c - also the solution, so we can search only one case
    • c = 1000 - a - b; It's one of the conditions of the problem (we don't need to scan all possible 'c': just calculate it)
    0 讨论(0)
  • 2020-12-24 14:01
    for a in range(1,334):
        for b in range(500, a, -1):
            if a + b < 500:
                break
            c = 1000 - a - b
            if a**2 + b**2 == c**2:
                print(a,b,c)
    

    Further optimization from Oleg's answer. One side cannot be greater than the sum of the other two. So a + b cannot be less than 500.

    0 讨论(0)
  • 2020-12-24 14:02

    From man pow:

    POW(3)                                       Linux Programmer's Manual                                      POW(3)
    
    NAME
           pow, powf, powl - power functions
    
    SYNOPSIS
           #include <math.h>
    
           double pow(double x, double y);
           float powf(float x, float y);
           long double powl(long double x, long double y);
    
           Link with -lm.
    
       Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
    
           powf(), powl(): _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE; or cc -std=c99
    
    DESCRIPTION
           The pow() function returns the value of x raised to the power of y.
    
    RETURN VALUE
           On success, these functions return the value of x to the power of y.
    
           If  x  is  a  finite  value less than 0, and y is a finite non-integer, a domain error occurs, and a NaN is
           returned.
    
           If the result overflows, a range error occurs, and the functions return HUGE_VAL, HUGE_VALF, or  HUGE_VALL,
    

    as you see, pow is using floating point arithmetic, which is unlikely to give you the exact result (although in this case should be OK, as relatively small integers have an exact representation; but don't rely on that for general cases)... use n*n to square the numbers in integer arithmetic (also, in modern CPU's with powerful floating point units the throughput can be even higher in floating point, but converting from integer to floating point has a very high cost in number of CPU cycles, so if you're dealing with integers, try to stick to integer arithmetic).

    some pseudocode to help you optimise a little bit your algorithm:

    for a from 1 to 998:
        for b from 1 to 999-a:
            c = 1000 - a - b
            if a*a + b*b == c*c:
                 print a, b, c
    
    0 讨论(0)
  • 2020-12-24 14:04

    In C the ^ operator computes bitwise xor, not the power. Use x*x instead.

    0 讨论(0)
  • 2020-12-24 14:05
    #include <stdio.h>
    
    int main() // main always returns int!
    {
     int a, b, c;
     for (a = 0; a<=1000; a++)
     {
      for (b = a + 1; b<=1000; b++) // no point starting from 0, otherwise you'll just try the same solution more than once. The condition says a < b < c.
      {
       for (c = b + 1; c<=1000; c++) // same, this ensures a < b < c.
       {
        if (((a*a + b*b == c*c) && ((a+b+c) ==1000))) // ^ is the bitwise xor operator, use multiplication for squaring
         printf("a=%d, b=%d, c=%d",a,b,c);
       }
      }
     }
     return 0;
    }
    

    Haven't tested this, but it should set you on the right track.

    0 讨论(0)
  • 2020-12-24 14:05

    As others have mentioned you need to understand the ^ operator. Also your algorithm will produce multiple equivalent answers with the parameters a,b and c in different orders.

    0 讨论(0)
提交回复
热议问题