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

PythonServer Side ProgrammingProgramming

Suppose we have the hypotenuse and area of a right angle triangle, we have to find the base and height of this triangle. If it is not possible return False.

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

To solve this, we will follow these steps −

• hypo_sq := hypo * hypo
• s := square root of (hypo_sq / 2.0)
• maxArea := calculate area of triangle using base s and hypotenuse hypo
• if area > maxArea, then
• return False
• left := 0.0, right := s
• while |right - left| > 0.000001, do
• base := (left + right) / 2.0
• if area of triangle using base s and hypotenuse hypo >= area, then
• right := base
• otherwise,
• left := base
• height := square root of (hypo_sq - base*base) and round of to nearest integer
• round of to nearest integer of base
• return base and height

Let us see the following implementation to get better understanding −

## Example Code

Live Demo

from math import sqrt

def calculate_area(b, h):
hei = sqrt(h*h - b*b);
return 0.5 * b * hei

def solve(hypo, area):
hypo_sq = hypo * hypo
s = sqrt(hypo_sq / 2.0)
maxArea = calculate_area(s, hypo)

if area > maxArea:
return False

left = 0.0
right = s

while abs(right - left) > 0.000001:
base = (left + right) / 2.0
if calculate_area(base, hypo) >= area:
right = base
else:
left = base

height = round(sqrt(hypo_sq - base*base))
base = round(base)
return base, height

hypo = 10
area = 24
print(solve(hypo, area))

## Input

10, 24

## Output

(6, 8)