Dado que aquí hay un octágono regular de longitud de lado a , la tarea es encontrar la longitud de su diagonal.
Ejemplos:
Input: a = 4 Output: 10.4525 Input: a = 5 Output: 13.0656
Enfoque : Del diagrama está claro que,
AB ^ 2 + BC ^ 2 = AC ^ 2
aquí, en el triángulo AED,
b ^ 2 + b ^ 2 = a ^ 2
o, b = a / √ 2 ( consulte )
Entonces, AB = a + 2b = a + √2a
y, BC = a
Entonces, diagonal AC = a√(4 + 2√2)
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to find the diagonal // of the octagon #include <bits/stdc++.h> using namespace std; // Function to find the diagonal // of the octagon float octadiagonal(float a) { // side cannot be negative if (a < 0) return -1; // diagonal of the octagon return a * sqrt(4 + (2 * sqrt(2))); } // Driver code int main() { float a = 4; cout << octadiagonal(a) << endl; return 0; }
Java
// Java Program to find the diagonal // of the octagon import java.util.*; class solution { // Function to find the diagonal // of the octagon static double octadiagonal(double a) { // side cannot be negative if (a < 0) return -1; // diagonal of the octagon return a * Math.sqrt(4 + (2 * Math.sqrt(2))); } // Driver code public static void main(String args[]) { double a = 4; System.out.println( octadiagonal(a)); } } //contributed by Arnab Kundu
Python3
# Python3 Program to find the diagonal # of the octagon import math # Function to find the diagonal # of the octagon def octadiagonal(a): # side cannot be negative if (a < 0): return -1; # diagonal of the octagon return a * math.sqrt(4 + (2 * math.sqrt(2))) # Driver code if __name__=='__main__': a = 4 print (octadiagonal(a)) # This code is contributed by # Shivi_Aggarwal
C#
// C# Program to find the diagonal // of the octagon using System; class GFG { // Function to find the diagonal // of the octagon static double octadiagonal(double a) { // side cannot be negative if (a < 0) return -1; // diagonal of the octagon return a * Math.Sqrt(4 + (2 * Math.Sqrt(2))); } // Driver code public static void Main() { double a = 4; Console.WriteLine(octadiagonal(a)); } } // This code is contributed // by inder_verma
PHP
<?php // PHP program to find the diagonal // of the octagon // Function to find the diagonal // of the octagon function octadiagonal($a) { // side cannot be negative if ($a < 0) return -1; // diagonal of the octagon return $a * sqrt(4 + (2 * sqrt(2))); } // Driver code $a = 4; echo octadiagonal($a) ; // This code is contributed // by inder_verma ?>
Javascript
<script> // javascript Program to find the diagonal // of the octagon // Function to find the diagonal // of the octagon function octadiagonal(a) { // side cannot be negative if (a < 0) return -1; // diagonal of the octagon return a * Math.sqrt(4 + (2 * Math.sqrt(2))); } var a = 4; document.write( octadiagonal(a).toFixed(5)); // This code is contributed by 29AjayKumar </script>
Producción:
10.4525
Complejidad del tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA