Given here is an equilateral triangle of side length a. The task is to find the side of the biggest square that can be inscribed within it.
Examples:
Input: a = 5
Output: 2.32
Input: a = 7
Output: 3.248

Approach: Let the side of the square be x.
Now, AH is perpendicular to DE.
DE is parallel to BC, So, angle AED = angle ACB = 60
In triangle EFC,
=> Sin60 = x/ EC
=> ?3 / 2 = x/EC
=> EC = 2x/?3
In triangle AHE,
=> Cos 60 = x/2AE
=> 1/2 = x/2AE
=> AE = x
So, side AC of the triangle = 2x/?3 + x. Now,
a = 2x/?3 + x
Therefore, x = a/(1 + 2/?3) = 0.464a
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float square( float a)
{
if (a < 0)
return -1;
float x = 0.464 * a;
return x;
}
int main()
{
float a = 5;
cout << square(a) << endl;
return 0;
}
|
Java
class GFG
{
static double square( double a)
{
if (a < 0 )
return - 1 ;
double x = 0.464 * a;
return x;
}
public static void main(String []args)
{
double a = 5 ;
System.out.println(square(a));
}
}
|
Python3
def square( a ):
if (a < 0 ):
return - 1
x = 0.464 * a
return x
a = 5
print (square(a))
|
C#
using System;
class GFG
{
static double square( double a)
{
if (a < 0)
return -1;
double x = 0.464 * a;
return x;
}
public static void Main()
{
double a = 5;
Console.WriteLine(square(a));
}
}
|
PHP
<?php
function square( $a )
{
if ( $a < 0)
return -1;
$x = 0.464 * $a ;
return $x ;
}
$a = 5;
echo square( $a );
?>
|
Javascript
<script>
function square(a)
{
if (a < 0)
return -1;
var x = 0.464 * a;
return x;
}
var a = 5;
document.write(square(a).toFixed(2));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Jun, 2022
Like Article
Save Article