(Pythagorean Triples) A right triangle can have sides that are all integers. A set of three in- teger values for the sides of a right triangle is called a Pythagorean triple. These three sides must sat- isfy 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 side1, side2 and 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’ll learn in more advanced computer science courses that there are many interesting problems for which there is no known algorithmic approach other than sheer brute force.

What will be an ideal response?

```
// Find Pythagorean triples using brute force computing.
#include
using namespace std;

int main()
{
int count = 0; // number of triples found
long int hypotenuseSquared; // hypotenuse squared
long int sidesSquared; // sum of squares of sides

cout << "Side 1\tSide 2\tSide3" << endl;

// side1 values range from 1 to 500
for ( long side1 = 1; side1 <= 500; side1++ )
{
// side2 values range from current side1 to 500
for ( long side2 = side1; side2 <= 500; side2++ )
{
// hypotenuse values range from current side2 to 500
for ( long hypotenuse = side2; hypotenuse <= 500; hypotenuse++ )
{
// calculate square of hypotenuse value
hypotenuseSquared = hypotenuse * hypotenuse;

// calculate sum of squares of sides
sidesSquared = side1 * side1 + side2 * side2;

// if (hypotenuse)^2 = (side1)^2 + (side2)^2,
// Pythagorean triple
if ( hypotenuseSquared == sidesSquared )
{
// display triple
cout << side1 << '\t' << side2 << '\t'
<< hypotenuse << '\n';
count++; // update count
} // end if
} // end for
} // end for
} // end for

// display total number of triples found
cout << "A total of " << count << " triples were found." << endl;
} // end main
```

Computer Science & Information Technology

You might also like to view...

In order to keep the original height and width proportions, you must make sure that the Lock ________ checkbox is selected

A) adjustment handle B) Smart Guide C) Quick Style D) aspect ratio

Computer Science & Information Technology

?Drop caps look better if the first letter's _____.

A. ?line height is increased B. ?line height is decreased C. ?width is decreased D. ?width is increased

Computer Science & Information Technology