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.
1# Take three numbers2a = int(input("Enter first number: "))3b = int(input("Enter second number: "))4c = int(input("Enter third number: "))56# Check all possible arrangements7if (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
Read the three integers.
- 2
Evaluate all three arrangements of the squared equation.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum