Given a perimeter P, the task is to find the number of right triangles possible with perimeter equal to p.
Input: P = 12 Output: number of right triangles = 1 The only right angle possible is with sides hypotenuse = 5, perpendicular = 4 and base = 3. Input: p = 840 Output: number of right triangles = 8
So the aim is to find the number of solutions which satisfy equations a + b + c = p and a2 + b2 = c2.
A naive approach is to run two loops for a(1 to p/2) and b(a+1 to p/3) then make c=p-a-b and increase count by one if
An efficient approach can be found by little algebraic manipulation :
Since a + c > b or, p – b > b or, b < p/2. Thus iterating b from 1 to p/2, calculating a and storing only the whole number a would give all solutions for a given p. There are no right triangles are possible for odd p as right angle triangles follow the Pythagoras theorem. Use a list of pairs to store the values of a and band return the count at the end.
Below is the implementation of the above approach.
number of right triangles = 8
Time complexity: O(P)
- Program to find all possible triangles having same Area and Perimeter
- Count number of triangles possible for the given sides range
- Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
- Count of Equilateral Triangles of unit length possible from a given Hexagon
- Count the number of possible triangles
- Count number of triangles possible with length of sides not exceeding N
- Maximum area of rectangle possible with given perimeter
- Number of triangles possible with given lengths of sticks which are powers of 2
- Number of possible Triangles in a Cartesian coordinate system
- Count number of triangles cut by the given horizontal and vertical line segments
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle
- Count the total number of triangles after Nth operation
- Count number of Unique Triangles using Operator overloading
- Program to calculate area and perimeter of a rhombus whose diagonals are given
- Perimeter of Convex hull for a given set of points
- Find maximum volume of a cuboid from the given perimeter and area
- Number of triangles that can be formed with given N points
- Python - Find the maximum number of triangles with given points on three lines
- Count triangles required to form a House of Cards of height N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : siddhanthapliyal