Open In App

Python Program to Check if right triangle possible from given area and hypotenuse

Improve
Improve
Like Article
Like
Save
Share
Report

A triangle is said to as having “right angles” if one of its angles is a right angle. The “legs” of a triangle are the two sides that cross over one another to make a right angle. The “hypotenuse” side of a triangle is the side that faces the right angle formed by the triangle. In this article, we’ll provide the right triangle’s legs and use Python to get the triangle’s hypotenuse.

Given the “legs” of a Right Triangle, return its Hypotenuse in Python

 

Returning the Hypotenuse of a Right Triangle with Pythagoras Theorem using Python

We are going to solve this problem with the help of Pythagoras’ Theorem. If we have two sides of a right-angled triangle, we can calculate the third side of the triangle with Pythagoras’ Theorem. It states that “The sum of squares of the two sides of a triangle is equal to the square of the hypotenuse side.”

h2 = a2 + b2
or
h = √(a2 + b2)

Example:

We’ll first of all import the “sqrt” function from the math module in Python as we will need it when dealing with calculating the square root of a^2 + b^2. Then we will define a function with two arguments which will be the arguments for the legs of the right triangle. After this, we’ll create a variable h and store the square root of a^2 + b^2 via the “sqrt” function and return our variable h.

Python3




# importing sqrt function from math module
from math import sqrt
 
# defining function 'hypotenuse_side' with a and b as its
# arguments
def hypotenuse_side(a, b):
    # creating variable 'h' with the formula
    h = sqrt(a**2 + b**2)
    return h
 
# driver program to test the above function
if __name__ == '__main__':
    a = 3
    b = 4
    print(hypotenuse_side(a, b))


Output:

5.0

Time complexity: O(1)

Space complexity: O(1)

What is the numpy.hypot() function in Python

The numpy.hypot() function is used to get the hypotenuse of the given legs. This function takes two arguments and returns the hypotenuse side assuming those arguments to be the two legs of the triangle. 

Syntax: numpy.exp2(arr1, arr2[, out]) = ufunc ‘hypot’) 

Returning the Hypotenuse of a Right Triangle using numpy.hypot() with 1D arrays

Example 1:

Here, we will be first importing the NumPy library as np. Then, we will be creating two one-dimensional arrays, 1st array containing leg 1 of the right triangle and 2nd array containing leg 2 of the right triangle. Then we will use the numpy.hypot() function with those two arrays as its parameters which will return the hypotenuse side.

Python3




# importing NumPy library as np (alias)
import numpy as np
 
# creating a 1-dimensional array that contains leg 1 of the triangle
a = np.array([3])
 
# creating another 1-dimensional array that contains leg 2 of the triangle
b = np.array([4])
 
# using numpy.hypot() function with arrays a and b as its parameters.
# This will return the hypotenuse side with the help of given parameters.
print(np.hypot(a, b))


Output:

[5.]

Time complexity: O(1)

Space complexity: O(1)

Example 2:

In the example below, we will again have two 1D arrays but this time, instead of having a single element in each array, we will be entering 3 values in each array. When we are performing this type of operation, we are assuming that we have 3 triangles. Then, we will finally use our numpy.hypot() function and pass those two arrays in this function as parameters. After doing this, we will get a 1-dimensional array with 3 elements as the 3 hypotenuse sides of 3 respective triangles.

Python3




# importing NumPy library as np
import numpy as np
 
# creating array 'leg_1' which contains leg 1 of all the 3 triangles
leg_1 = np.array([3, 6, 5])
 
# creating array 'leg_2' which contains leg 2 of all the 3 triangles
leg_2 = np.array([4, 8, 12])
 
# printing the hypotenuse sides of all the triangles
print(np.hypot(leg_1, leg_2))


Output:

[ 5. 10. 13.]

Time complexity: O(n)

Space complexity: O(n)), n is no.of elements in 2D array

Example 3

Here, we will create only one 1D array, in which we will store 2 elements, leg 1 and leg 2 of our right triangle. Then we will use the numpy.hypot() function and pass index 0 of the created array as its first parameter and index 1 of the same array as its second parameter.

Python3




# importing NumPy library as np
import numpy as np
 
# creating numpy array containing leg 1 and leg 2 of the triangle.
legs = np.array([3, 4])
 
# using numpy.hypot() function with 1st element of "legs" array being its 1st parameter
# and 2nd element of "legs" array being its 2nd parameter.
print(np.hypot(legs[0], legs[1]))


Output

5.0

Time complexity: O(1)

Space complexity: O(1)

Returning the hypotenuse of a Right Triangle using numpy.hypot() with 2D arrays

Example 1:

Here, we will be calculating the hypotenuse side of a triangle with 2-dimensional arrays. First of all, we will import the NumPy library Then, we will create two 2-dimensional arrays containing leg 1 and leg 2 respectively. Finally, we will use numpy.hypot() with those two arrays as its parameter.

Python3




# importing hypot and array function from NumPy library
from numpy import hypot, array
 
# creating two 2 dimensional arrays containing leg 1 and leg 2
leg1 = np.array([[3]])
leg2 = np.array([[4]])
 
# using numpy.hypot() function with leg1 and leg2 as its parameters
print(hypot(leg1, leg2))


Output

[[5.]]

Time complexity: O(1)

Space complexity: O(1

Example 2:

We can also have more than one row in the 2D array that contains an element. Consider the following example. Let’s say we need to find the hypotenuse side of three different triangles. We’ll start by importing the NumPy library. Then we’ll make two 2D arrays. containing the first and second legs of each of the three triangles We have three legs to store in the leg 1 array. We’ll make three rows to store each leg. The leg 2 array will be treated similarly. Finally, we’ll use the numpy.hypot() function to obtain the hypotenuse sides.

Python3




# importing hypot and array function from NumPy library
from numpy import hypot, array
 
# creating two 2 dimensional arrays containing leg 1 and leg 2
leg1 = array([[3], [6], [5]])
leg2 = array([[4], [8], [12]])
 
# using numpy.hypot() function with leg1 and leg2 as its parameters
print(hypot(leg1, leg2))


Output

[[ 5.]
 [10.]
 [13.]]

Time complexity: O(n) 

Space complexity: O(n) , n is no.of elements in 2D array



Last Updated : 13 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads