85,808 views
6 votes
6 votes
The set of three integer values for the lengths of the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the two sides is equal to the square of the hypotenuse. Find all integer Pythagorean triples for side1, side2, and the hypotenuse, all no larger than 500. Use a triple-nested for loop that tries all possibilities. This program 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 algorithmic approach other than using sheer brute force. This program does not need any input from the user. Write at least one bool function that takes the 3 sides of the triangle and returns true or false based on if it is a right triangle or not.

User Achille G
by
2.6k points

1 Answer

14 votes
14 votes

Answer:

In Python:

def Pythagorean_triple(hyp,side1,side2):

if hyp**2 == side1**2 + side2*2:

return True

else:

return False

print("Hypotenuse\tSide 1\t Side 2\t Return Value")

for i in range(1,501):

for j in range(1,501):

for k in range(1,501):

print(str(i)+"\t"+str(j)+"\t"+str(k)+"\t"+str(Pythagorean_triple(i,j,k)))

Step-by-step explanation:

This defines the function

def Pythagorean_triple(hyp,side1,side2):

This checks for pythagorean triple

if hyp**2 == side1**2 + side2*2:

Returns True, if true

return True

else:

Returns False, if otherwise

return False

The main method begins

This prints the header

print("Hypotenuse\tSide 1\t Side 2\t Return Value")

The following is a triple-nested loop [Each of the loop is from 1 to 500]

for i in range(1,501): -->The hypotenuse

for j in range(1,501): -->Side 1

for k in range(1,501):-->Side 2

This calls the function and prints the required output i.e. the sides of the triangle and True or False

print(str(i)+"\t"+str(j)+"\t"+str(k)+"\t"+str(Pythagorean_triple(i,j,k)))

User John Wigger
by
2.8k points