Area of a square inscribed in a circle which is inscribed in an equilateral triangle
Last Updated :
27 Aug, 2022
Given here is an equilateral triangle with side length a, which inscribes a circle, which in turn inscribes a square. The task is to find the area of this square.
Examples:
Input: a = 6
Output: 1
Input: a = 10
Output: 0.527046
Approach:
let r be the radius of circle,
hence it is the inradius of equilateral triangle, so r = a /(2 * ?3)
diagonal of square, d = diameter of circle = 2 * r = a/ ?3
So, area of square, A = 0.5 * d * d
hence A = (1/2) * (a^2) / (3) = (a^2/6)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float area( float a)
{
if (a < 0)
return -1;
float area = sqrt (a) / 6;
return area;
}
int main()
{
float a = 10;
cout << area(a) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static float area( float a)
{
if (a < 0 )
return - 1 ;
float area = ( float )Math.sqrt(a) / 6 ;
return area;
}
public static void main (String[] args) {
float a = 10 ;
System.out.println( area(a));
}
}
|
Python 3
from math import *
def area(a):
if a < 0 :
return - 1
area = sqrt(a) / 6
return area
if __name__ = = "__main__" :
a = 10
print ( round (area(a), 6 ))
|
C#
using System;
class GFG
{
static float area( float a)
{
if (a < 0)
return -1;
float area = ( float )Math.Sqrt(a) / 6;
return area;
}
public static void Main ()
{
float a = 10;
Console.WriteLine(area(a));
}
}
|
PHP
<?php
function area( $a )
{
if ( $a < 0)
return -1;
$area = sqrt( $a ) / 6;
return $area ;
}
$a = 10;
echo area( $a );
?>
|
Javascript
<script>
function area(a)
{
if (a < 0)
return -1;
var area = Math.sqrt(a) / 6;
return area;
}
var a = 10;
document.write( area(a).toFixed(6));
</script>
|
Time complexity : O(log(a)) for given side a, as complexity of inbuilt sqrt function
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...