(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 side1, side2 and hypotenuse all no larger than 20.
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 many interesting problems for which there is no known algorithmic approach other than using sheer brute force.
What will be an ideal response?
```
# Pythagorean triplets.
# test 20 possible hypotenuses
for hypotenuse in range ( 1, 21 ):
hypotenuseSquared = hypotenuse * hypotenuse
# test 20 possibilities for side1
for side1 in range ( 1, 21 ):
# test 20 possibilities for side2
for side2 in range ( 1, 21 ):
side1squared = side1 ** 2
side2squared = side2 ** 2
# if the sum of the squares of the two sides is equal
# to the square of the hypotenuse, display Pythagorean
# triplet
if hypotenuseSquared == side1squared + side2squared:
print "hypotenuse =", hypotenuse
print "side1 =", side1
print "side2 =", side2
```
hypotenuse =
5
side1 =
3
side2 =
4
hypotenuse =
5
side1 =
4
side2 =
3
hypotenuse = 10
side1 =
6
side2 =
8
hypotenuse = 10
side1 =
8
side2 =
6
hypotenuse = 13
side1 =
5
side2 = 12
hypotenuse = 13
side1 = 12
side2 =
5
hypotenuse = 15
side1 =
9
side2 = 12
hypotenuse = 15
side1 = 12
side2 =
9
hypotenuse = 17
side1 =
8
side2 = 15
hypotenuse = 17
side1 = 15
side2 =
8
hypotenuse = 20
side1 = 12
side2 = 16
hypotenuse = 20
side1 = 16
side2 = 12
You might also like to view...
Assume proper includes have been executed, but not using directive or declaration. Write a definition of an iterator for a vector named vec of int values. Write a for loop that will display the contents vec on the screen, separated by spaces starting at the last element in the vector and proceeding to the first. Use iterators for the loop control.
What will be an ideal response?
What’s the difference between a linear gradient and a radial gradient?
What will be an ideal response?
Which of the following will create a String different from the other three?
a. String r = "123456" b. int i = 123; int j = 456; String r = String.valueOf(j) + String.valueOf(i); c. int i = 123; int j = 456; String r = String.valueOf(i) + String.valueOf(j); d. int i = 123; int j = 456; String r = i + j;
________ have consistent formatting, and are used to exchange information between computer applications
Fill in the blank(s) with correct word