Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Check Pythagorean Triplet

Test three integers against the Pythagorean equation. Students examine all possible longest-side positions and the limits of an unchecked numeric input.

AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 2 of 10
check-pythagorean-triplet.py
1# Take three numbers
2a = int(input("Enter first number: "))
3b = int(input("Enter second number: "))
4c = int(input("Enter third number: "))
5
6# Check all possible arrangements
7if (a*a + b*b == c*c) or (b*b + c*c == a*a) or (c*c + a*a == b*b):
8 print("Pythagorean triplet")
9else:
10 print("Not a Pythagorean triplet")

Output

Enter first number: 3
Enter second number: 4
Enter third number: 5
Pythagorean triplet

Enter first number: 2
Enter second number: 3
Enter third number: 4
Not a Pythagorean triplet

In this one

  • Test a squared relation
  • Try every longest-side position
  • Separate algebra from side validity

How it works

  1. 1

    Read the three integers.

  2. 2

    Evaluate all three arrangements of the squared equation.

  3. 3

    Accept when any arrangement is true.

Edge cases

Do reordered sides work?

Do reordered sides work?

Yes. All three possible longest-side positions are tested.

Does 0, 0, 0 pass the equation?

Does 0, 0, 0 pass the equation?

Yes, although it is not a valid triangle; side validation is separate.

What's going on

The equation a squared plus b squared equals c squared describes a right triangle when the side lengths are positive. The code tries every possible position for the squared longest side.

Three equation checks are joined with or, so the inputs need not be sorted. Both 3, 4, 5 and 5, 3, 4 satisfy an arrangement. However, the code checks equations only; it does not enforce positive, nonzero side lengths.

Do reordered sides work? Yes. All three possible longest-side positions are tested.

Does 0, 0, 0 pass the equation? Yes, although it is not a valid triangle; side validation is separate.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which triple passes with positive sides?

Pick an answer to check it.

Question 2

Do reordered sides work?

Pick an answer to check it.

Questions people ask

Do reordered sides work?

Yes. All three possible longest-side positions are tested.

Does 0, 0, 0 pass the equation?

Yes, although it is not a valid triangle; side validation is separate.

Which triple passes with positive sides?

3, 4, 5.