Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
Last Updated :
07 Aug, 2022
Given here is an equilateral triangle of side length a, which inscribes a hexagon which in turn inscribes a square. The task is to find the side length of the square.
Examples:
Input: a = 6
Output: 2.538
Input: a = 8
Output: 3.384
Approach:
We know the, side length of a hexagon inscribed within an equilateral triangle is h = a/3. Please refer Largest hexagon that can be inscribed within an equilateral triangle .
Also, the side length of the square that can be inscribed within a hexagon is x = 1.268h Please refer Largest Square that can be inscribed within a hexagon.
So, side length of the square inscribed within a hexagon which in turn is inscribed within an equilateral triangle, x = 0.423a.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float squareSide( float a)
{
if (a < 0)
return -1;
float x = 0.423 * a;
return x;
}
int main()
{
float a = 8;
cout << squareSide(a) << endl;
return 0;
}
|
Java
class cfg
{
static float squareSide( float a)
{
if (a < 0 )
return - 1 ;
float x = ( 0 .423f * a);
return x;
}
public static void main(String[] args)
{
float a = 8 ;
System.out.println(squareSide(a));
}
}
|
Python3
def squareSide(a):
if (a < 0 ):
return - 1
x = 0.423 * a
return x
if __name__ = = '__main__' :
a = 8
print (squareSide(a))
|
C#
using System;
class GFG
{
static float squareSide( float a)
{
if (a < 0)
return -1;
float x = (0.423f * a);
return x;
}
public static void Main()
{
float a = 8;
Console.WriteLine(squareSide(a));
}
}
|
PHP
<?php
function squareSide( $a )
{
if ( $a < 0)
return -1;
$x = 0.423 * $a ;
return $x ;
}
$a = 8;
echo squareSide( $a );
?>
|
Javascript
<script>
function squareSide(a)
{
if (a < 0)
return -1;
var x = (0.423 * a);
return x;
}
var a = 8;
document.write(squareSide(a));
</script>
|
Time Complexity: O(1) since no loop is used the algorithm takes constant time to finish its execution
Auxiliary Space: O(1) since no extra array is used the space required by the algorithm to complete is constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...