Python Program to Check if right triangle possible from given area and hypotenuse
Last Updated :
05 Sep, 2024
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.
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.
Python
# 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.
Python
# 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.
Python
# 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.
Python
# 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.
Python
# 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.
Python
# 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
Similar Reads
Python Program to Calculate the Area of a Triangle
A triangle is a closed-shaped two-dimensional polygon having three sides and three corners. The corners are called vertices and the sides are called edges. In this article, we will see how we can calculate the area of a triangle in Python. Using Heron's FormulaWhen the lengths of all three sides of
2 min read
Check if a triangle of positive area is possible with the given angles
Given three angles. The task is to check if it is possible to have a triangle of positive area with these angles. If it is possible print "YES" else print "NO".Examples: Input : ang1 = 50, ang2 = 60, ang3 = 70 Output : YES Input : ang1 = 50, ang2 = 65, ang3 = 80 Output : NO Approach: We can form a v
5 min read
Python Program to check if matrix is lower triangular
Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 0, 0, 0}, {1, 4, 0, 0}, {4, 6, 2, 0}, {0, 4, 7, 6}};Output : Matrix is in l
2 min read
Python Program to check if matrix is upper triangular
Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}}; Output : Matrix is in
2 min read
Python Program to Find Area of a Circle
The task of calculating the Area of a Circle in Python involves taking the radius as input, applying the mathematical formula for the area of a circle and displaying the result. Area of a circle formula: Area = pi * r2where Ï (pi) is a mathematical constant approximately equal to 3.14159. r is the r
3 min read
Python Program for Maximum height when coins are arranged in a triangle
We have N coins which need to arrange in form of a triangle, i.e. first row will have 1 coin, second row will have 2 coins and so on, we need to tell maximum height which we can achieve by using these N coins. Examples: Input : N = 7Output : 3Explanation: Maximum height will be 3, putting 1, 2 and t
3 min read
Python Program to Find Area of Rectangle
The task of calculating the Area of a Rectangle in Python involves taking the length and width as input, applying the mathematical formula for the area of a rectangle, and displaying the result. Area of Rectangle Formula :Area = Width * Height Where: Length is the longer side of the rectangle.Width
2 min read
Python Program for Program to calculate area of a Tetrahedron
A Tetrahedron is simply a pyramid with a triangular base. It is a solid object with four triangular faces, three on the sides or lateral faces, one on the bottom or the base and four vertices or corners. If the faces are all congruent equilateral triangles, then the tetrahedron is called regular. Th
2 min read
Python Program to find volume, surface area and space diagonal of a cuboid
Given the length, base, and height of a cuboid. The task is to find the Surface Area, Volume and space diagonal of the cuboid. Examples: Input : length = 9 breadth = 6 height = 10 Output : Surface area = 408 volume = 540 space diagonal = 14.73 Input : length = 5 breadth = 4 height = 3 Output : surfa
2 min read
Python | Check if all the values in a list are less than a given value
When working with lists in Python, we might need to verify if all elements in a list are less than a specified value. In this article, we will explore several methods to perform this check efficiently. Using all() Functionall() function is a built-in Python method that checks if all elements in an i
3 min read