Triangle Inequality Checker
Triangles are fundamental geometric shapes with many applications in fields ranging from computer graphics to structural engineering. A key property of triangles is the "triangle inequality theorem," which states that the sum of the lengths of any two sides of a triangle must be greater than the length of the third side. This challenge requires you to implement a function that verifies if three given lengths can form a valid triangle.
Problem Description
Your task is to create a function that takes three positive numbers, representing the lengths of three potential sides of a triangle, and determines if these lengths can form a valid triangle.
Requirements:
- The function should accept three numerical inputs.
- The function should return a boolean value:
trueif the lengths can form a triangle, andfalseotherwise. - The triangle inequality theorem must be applied: for sides A, B, and C, the following must all be true:
- A + B > C
- A + C > B
- B + C > A
- All input lengths are assumed to be positive.
Expected Behavior:
- If all three conditions of the triangle inequality theorem are met, the function should return
true. - If any of the conditions are not met, the function should return
false.
Edge Cases:
- Consider cases where two sides sum up to exactly the length of the third side (e.g., 3, 4, 7). This should result in
falseas it forms a degenerate triangle (a straight line). - The problem statement guarantees positive inputs, so you do not need to explicitly check for zero or negative lengths.
Examples
Example 1:
Input: side1 = 3, side2 = 4, side3 = 5
Output: true
Explanation:
3 + 4 > 5 (7 > 5) - True
3 + 5 > 4 (8 > 4) - True
4 + 5 > 3 (9 > 3) - True
All conditions are met.
Example 2:
Input: side1 = 1, side2 = 2, side3 = 5
Output: false
Explanation:
1 + 2 > 5 (3 > 5) - False
Since one condition is false, the lengths cannot form a triangle.
Example 3:
Input: side1 = 7, side2 = 10, side3 = 5
Output: true
Explanation:
7 + 10 > 5 (17 > 5) - True
7 + 5 > 10 (12 > 10) - True
10 + 5 > 7 (15 > 7) - True
All conditions are met.
Example 4: (Edge Case)
Input: side1 = 2, side2 = 3, side3 = 5
Output: false
Explanation:
2 + 3 > 5 (5 > 5) - False
This represents a degenerate triangle (a straight line), which is not considered a valid triangle in this context.
Constraints
- The input side lengths will be positive integers.
- The maximum value for any side length will be 1000.
- The function should execute efficiently and complete within typical time limits for competitive programming or standard application development.
Notes
- Focus on correctly implementing the triangle inequality theorem.
- Pseudocode is provided for reference, but you should implement the solution in your preferred programming language.
Pseudocode:
FUNCTION canFormTriangle(side1, side2, side3):
IF (side1 + side2 > side3) AND (side1 + side3 > side2) AND (side2 + side3 > side1):
RETURN true
ELSE:
RETURN false
END IF
END FUNCTION