Given an N-sided polygon, the task is to find the ratio of the area of the Nth to (N + 1)th N-sided regular nested polygons generated by joining the midpoints of the sides of the original polygon.
Examples :
Input: N = 3
Output: 4.000000
Explanation:

Nested Triangle
Ratio of the length of the sides formed by joining the mid-points of the triangle with the length of the side of the original triangle is 0.5. Hence, R = (Area of Nth triangle) / (Area of (N + 1)th triangle) = 4
Input: N = 4
Output: 2.000000
Approach: The problem can be solved based on the following observations:
- Consider an N-sided regular polygon as shown in the figure below.

Representation Of Nested regular polygon of N sides.
- A = 2 * ? / N
B = ? / N
h = r * cos(B)
b = h * cos(B)
c = h((1 – cos(A)) / 2)1/2 - Area of the Black Isosceles Triangle:

- Area of the Red Isosceles Triangle:

- r = s / (2 * [1 – cos(2B)])1/2 and b = r * [cos(B)]2
- After combining the above equations:

- Final result obtained is as follows:

Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void AreaFactor( int n)
{
double pi = 3.14159265;
double areaf = 1 / ( cos (pi / n)
* cos (pi / n));
cout << fixed << setprecision(6)
<< areaf << endl;
}
int main()
{
int n = 4;
AreaFactor(n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void AreaFactor( int n)
{
double pi = 3.14159265 ;
double areaf = 1 / (Math.cos(pi / n) *
Math.cos(pi / n));
System.out.format( "%.6f" , areaf);
}
public static void main(String[] args)
{
int n = 4 ;
AreaFactor(n);
}
}
|
Python3
import math
def AreaFactor(n):
pi = 3.14159265
areaf = 1 / (math.cos(pi / n) *
math.cos(pi / n))
print ( '%.6f' % areaf)
if __name__ = = "__main__" :
n = 4
AreaFactor(n)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void AreaFactor( int n)
{
double pi = 3.14159265;
double areaf = 1 / (Math.Cos(pi / n) *
Math.Cos(pi / n));
Console.WriteLine(Math.Round(areaf));
}
public static void Main( string [] args)
{
int n = 4;
AreaFactor(n);
}
}
|
Javascript
<script>
function AreaFactor(n)
{
let pi = 3.14159265;
let areaf = (1 / (Math.cos(pi / n)
* Math.cos(pi / n)));
document.write(areaf.toFixed(6));
}
let n = 4;
AreaFactor(n);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)