Check if right triangle possible from given area and hypotenuse in Python

Given the hypotenuse and area of a right triangle, we need to find the base and height. If it's not possible to form such a triangle, we return False.

So, if the input is like hypo = 10, area = 24, then the output will be (6, 8).

Algorithm

To solve this, we will follow these steps −

  • Calculate the maximum possible area for the given hypotenuse
  • If the required area exceeds the maximum, return False
  • Use binary search to find the base that gives the required area
  • Calculate the corresponding height using the Pythagorean theorem

Understanding the Maximum Area

For a right triangle with a fixed hypotenuse, the maximum area occurs when the triangle is isosceles (base = height). In this case, both legs equal hypo / ?2.

base height hypotenuse Right Triangle

Implementation

from math import sqrt

def calculate_area(base, hypotenuse):
    """Calculate area of right triangle given base and hypotenuse"""
    height = sqrt(hypotenuse * hypotenuse - base * base)
    return 0.5 * base * height

def solve(hypo, area):
    """Find base and height of right triangle given hypotenuse and area"""
    hypo_sq = hypo * hypo
    s = sqrt(hypo_sq / 2.0)  # Base when triangle is isosceles
    maxArea = calculate_area(s, hypo)
    
    # Check if required area is possible
    if area > maxArea:
        return False
    
    # Binary search for the correct base
    left = 0.0
    right = s
    
    while abs(right - left) > 0.000001:
        base = (left + right) / 2.0
        current_area = calculate_area(base, hypo)
        
        if current_area >= area:
            right = base
        else:
            left = base
    
    # Calculate final base and height
    height = round(sqrt(hypo_sq - base * base))
    base = round(base)
    return base, height

# Test the function
hypo = 10
area = 24
result = solve(hypo, area)
print(f"Base and Height: {result}")
Base and Height: (6, 8)

Testing Edge Cases

Let's test when the triangle is not possible ?

# Test impossible case
hypo = 5
area = 20  # Too large for hypotenuse of 5

result = solve(hypo, area)
print(f"Result for impossible triangle: {result}")

# Test maximum area case (isosceles triangle)
hypo = 10
max_area = calculate_area(sqrt(50), 10)  # sqrt(100/2) = sqrt(50)
print(f"Maximum area for hypotenuse 10: {max_area:.2f}")

result = solve(hypo, max_area)
print(f"Isosceles triangle result: {result}")
Result for impossible triangle: False
Maximum area for hypotenuse 10: 25.00
Isosceles triangle result: (7, 7)

How It Works

The algorithm uses binary search because the area function is monotonic with respect to the base. As the base increases from 0 to the maximum value, the area first increases and then decreases, reaching maximum when base equals height.

Conclusion

This solution efficiently finds the base and height of a right triangle using binary search. The key insight is that the maximum area occurs when the triangle is isosceles, which helps us determine if the required area is achievable.

Updated on: 2026-03-25T14:27:46+05:30

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements