Solved

# Pythagorean Triples

Posted on 1997-03-06
572 Views

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.

0
Question by:fondue

LVL 4

Expert Comment

Is this a homework problem?
0

LVL 2

Accepted Solution

Slarti earned 10 total points
#include <iostream.h>
int main()
{
int a,b,c;
for(a=1;a<500;a++)
for(b=a;b<500;b++)
for(c=b;c<500;c++)
if(a*a+b*b==c*c)
cout<<a<<","<<b<<","<<c<<endl;
return 0;
}
0

Expert Comment

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

0

Expert Comment

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

0

LVL 84

Expert Comment

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;
}
}

0

## Featured Post

### Suggested Solutions

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.