Dados dos enteros r y n , donde n es el número de lados de un polígono regular y r es el radio del círculo en el que está circunscrito este polígono. La tarea es encontrar la longitud del lado del polígono.
Ejemplos:
Entrada: n = 5, r = 11
Salida: 12,9256
Entrada: n = 3, r = 5
Salida: 8,6576
Enfoque: Considere la imagen de arriba y deje que el ángulo AOB sea theta y luego theta = 360 / n .
En un triángulo rectángulo , el ángulo ACO = 90 grados y el ángulo AOC = theta/2 .
Entonces, AC = OA * sin(theta / 2) = r * sin(theta / 2)
Por lo tanto, lado del polígono, AB = 2 * AC es decir 2 * r * sin(theta / 2) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to calculate the side of the polygon // circumscribed in a circle float calculateSide(float n, float r) { float theta, theta_in_radians; theta = 360 / n; theta_in_radians = theta * 3.14 / 180; return 2 * r * sin(theta_in_radians / 2); } // Driver Code int main() { // Total sides of the polygon float n = 3; // Radius of the circumscribing circle float r = 5; cout << calculateSide(n, r); }
Java
// Java implementation of the approach import java.lang.Math; import java.io.*; class GFG { // Function to calculate the side of the polygon // circumscribed in a circle static double calculateSide(double n, double r) { double theta, theta_in_radians; theta = 360 / n; theta_in_radians = theta * 3.14 / 180; return 2 * r * Math.sin(theta_in_radians / 2); } // Driver Code public static void main (String[] args) { // Total sides of the polygon double n = 3; // Radius of the circumscribing circle double r = 5; System.out.println (calculateSide(n, r)); } //This code is contributed by akt_mit }
Python3
# Python 3 implementation of the approach from math import sin # Function to calculate the side of # the polygon circumscribed in a circle def calculateSide(n, r): theta = 360 / n theta_in_radians = theta * 3.14 / 180 return 2 * r * sin(theta_in_radians / 2) # Driver Code if __name__ == '__main__': # Total sides of the polygon n = 3 # Radius of the circumscribing circle r = 5 print('{0:.5}'.format(calculateSide(n, r))) # This code is contributed by # Sanjit_Prasad
C#
// C# implementation of the approach using System; class GFG { // Function to calculate the side of the polygon // circumscribed in a circle static double calculateSide(double n, double r) { double theta, theta_in_radians; theta = 360 / n; theta_in_radians = theta * 3.14 / 180; return Math.Round(2 * r * Math.Sin(theta_in_radians / 2),4); } // Driver Code public static void Main () { // Total sides of the polygon double n = 3; // Radius of the circumscribing circle double r = 5; Console.WriteLine(calculateSide(n, r)); } // This code is contributed by Ryuga }
PHP
<?php // PHP implementation of the approach // Function to calculate the side of the // polygon circumscribed in a circle function calculateSide($n, $r) { $theta; $theta_in_radians; $theta = 360 / $n; $theta_in_radians = $theta * 3.14 / 180; return 2 * $r * sin($theta_in_radians / 2); } // Driver Code // Total sides of the polygon $n = 3; // Radius of the circumscribing circle $r = 5; echo calculateSide($n, $r); // This code is contributed by inder_verma.. ?>
Javascript
<script> // javascript implementation of the approach // Function to calculate the side of the polygon // circumscribed in a circle function calculateSide( n , r) { var theta, theta_in_radians; theta = 360 / n; theta_in_radians = theta * 3.14 / 180; return 2 * r * Math.sin(theta_in_radians / 2); } // Driver Code // Total sides of the polygon var n = 3; // Radius of the circumscribing circle var r = 5; document.write(calculateSide(n, r).toFixed(5)); // This code contributed by Princi Singh </script>
8.6576
Complejidad Temporal: O(1), ya que no hay bucle ni recursividad.
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.