(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
```
You might also like to view...
List the order in which the steps that follow should be carried out during the development of a computer program to solve a problem:
a. Determine the data types of the memory cells required for storage of problem inputs and outputs. b. Determine the problem inputs and outputs. c. Read and understand the problem. d. Write an initial algorithm. e. Refine algorithm steps where needed.
Work breakdown structure properties are:
What will be an ideal response?
Which of the following terms is interchangeable with the term local override?
A. immediate change B. master change C. master override D. local change
As parameters to a function, class objects can be passed by reference only.
Answer the following statement true (T) or false (F)