Link to home
Start Free TrialLog in
Avatar of fondue
fondue

asked on

Pythagorean Triples


2.55      (Pythagorean Triples) A right triangle can have sides that are all integers.  The set of three integer values for the sides of a right triangle is called a Pythagorean triple.  These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse.  Find all Pythagorean triples for side l, side2, and the hypotenuse all no larger than 500.  Use a triple-nested for-loop that tries all possibilities.  This is an example of "brute force" computing.  You will learn in more advanced computer science courses that there are large numbers of interesting problems for which there is no known algorithmic approach other than using sheer brute force.

Avatar of jos010697
jos010697

Is this a homework problem?
ASKER CERTIFIED SOLUTION
Avatar of Slarti
Slarti

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
since 500 * 500 = 250000, you will have to use long ints, otherwise the above code won't work.


since 500 * 500 = 250000, you will have to use long ints, otherwise the above code won't work.


Avatar of ozo
Just so we don't give the impression that there is no known algorithmic approach other than using sheer brute force for this problem:

for( s=1; s<23; s++){
    for( t=s+1; (c = t*t + s*s) < 500; t++ ){
      a = t*t - s*s; b = 2*s*t;
        cout<<a<<","<<b<<","<<c<<endl;
    }
}