Find the dimensions of Right angled triangle
Given H (Hypotenuse) and A (area) of a right angled triangle, find the dimensions of right angled triangle such that the hypotenuse is of length H and its area is A. If no such triangle exists, print “Not Possible”.
Examples:
Input : H = 10, A = 24
Output : P = 6.00, B = 8.00
Input : H = 13, A = 36
Output : Not Possible
Approach:
Before moving to exact solution, let’s do some of mathematical calculations related to properties of Right-angled triangle.
Suppose H = Hypotenuse, P = Perpendicular, B = Base and A = Area of right angled triangle.
We have some sort of equations as :
P^2 + B^2 = H^2
P * B = 2 * A
(P+B)^2 = P^2 + B^2 + 2*P*B = H^2 + 4*A
(P+B) = sqrt(H^2 + 4*A) ----1
(P-B)^2 = P^2 + B^2 - 2*P*B = H^2 - 4*A
mod(P-B) = sqrt(H^2 - 4*A) ----2
from equation (2) we can conclude that if
H^2 < 4*A then no solution is possible.
Further from (1)+(2) and (1)-(2) we have :
P = (sqrt(H^2 + 4*A) + sqrt(H^2 - 4*A) ) / 2
B = (sqrt(H^2 + 4*A) - sqrt(H^2 - 4*A) ) / 2
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findDimen( int H, int A)
{
if (H * H < 4 * A) {
cout << "Not Possible\n" ;
return ;
}
double apb = sqrt (H * H + 4 * A);
double asb = sqrt (H * H - 4 * A);
cout.precision(2);
cout << "P = " << fixed
<< (apb - asb) / 2.0 << "\n" ;
cout << "B = " << (apb + asb) / 2.0;
}
int main()
{
int H = 5;
int A = 6;
findDimen(H, A);
return 0;
}
|
Java
class GFG {
static void findDimen( int H, int A)
{
if (H * H < 4 * A) {
System.out.println( "Not Possible" );
return ;
}
double apb = Math.sqrt(H * H + 4 * A);
double asb = Math.sqrt(H * H - 4 * A);
System.out.println( "P = " + Math.round(((apb - asb) / 2.0 ) * 100.0 ) / 100.0 );
System.out.print( "B = " + Math.round(((apb + asb) / 2.0 ) * 100.0 ) / 100.0 );
}
public static void main(String[] args)
{
int H = 5 ;
int A = 6 ;
findDimen(H, A);
}
}
|
Python3
from math import sqrt
def findDimen( H, A):
if H * H < 4 * A:
print ( "Not Possible" )
return
apb = sqrt(H * H + 4 * A)
asb = sqrt(H * H - 4 * A)
print ( "P = " , "%.2f" % ((apb - asb) / 2.0 ))
print ( "B = " , "%.2f" % ((apb + asb) / 2.0 ))
H = 5
A = 6
findDimen(H, A)
|
C#
using System;
class GFG {
static void findDimen( int H, int A)
{
if (H * H < 4 * A) {
Console.WriteLine( "Not Possible" );
return ;
}
double apb = Math.Sqrt(H * H + 4 * A);
double asb = Math.Sqrt(H * H - 4 * A);
Console.WriteLine( "P = " + Math.Round(
((apb - asb) / 2.0) * 100.0) / 100.0);
Console.WriteLine( "B = " + Math.Round(
((apb + asb) / 2.0) * 100.0) / 100.0);
}
public static void Main()
{
int H = 5;
int A = 6;
findDimen(H, A);
}
}
|
PHP
<?php
function findDimen( $H , $A )
{
if ( $H * $H < 4 * $A )
{
echo "Not Possible\n" ;
return ;
}
$apb = sqrt( $H * $H + 4 * $A );
$asb = sqrt( $H * $H - 4 * $A );
echo "P = " , $fixed
, ( $apb - $asb ) / 2.0 , "\n" ;
echo "B = " , ( $apb + $asb ) / 2.0;
}
$H = 5;
$A = 6;
findDimen( $H , $A );
?>
|
Javascript
<script>
function findDimen(H, A)
{
if (H * H < 4 * A)
{
document.write( "Not Possible" );
return ;
}
let apb = Math.sqrt(H * H + 4 * A);
let asb = Math.sqrt(H * H - 4 * A);
document.write( "P = " +((apb - asb) / 2.0).toFixed(2), "<br>" );
document.write( "B = " +((apb + asb) / 2.0).toFixed(2));
}
let H = 5;
let A = 6;
findDimen(H, A);
</script>
|
Output:
P = 3.00
B = 4.00
Time complexity : O(log(n)) since using inbuilt sqrt functions
Auxiliary Space : O(1)
Last Updated :
27 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...