El cubo más grande que se puede inscribir dentro de la esfera.

Dado que aquí hay una esfera de radio r , la tarea es encontrar el lado del cubo más grande que pueda caber dentro de ella.
Ejemplos: 
 

Input: r = 8
Output: 9.2376

Input: r = 5
Output: 5.7735

Enfoque :
 

Lado del cubo = a 
Radio de la esfera = r 
De la diagonal, es claro que, diagonal del cubo = diámetro de la esfera, 
a√3 = 2r o, a = 2r/√3

A continuación se muestra la implementación:
 

C++

// C++ Program to find the biggest cube
// inscribed within a sphere
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the side of the cube
float largestCube(float r)
{
 
    // radius cannot be negative
    if (r < 0)
        return -1;
 
    // side of the cube
    float a = (2 * r) / sqrt(3);
    return a;
}
 
// Driver code
int main()
{
    float r = 5;
    cout << largestCube(r) << endl;
 
    return 0;
}

Java

// Java Program to find the biggest cube
// inscribed within a sphere
import java.util.*;
class Solution{
// Function to find the side of the cube
static float largestCube(float r)
{
   
    // radius cannot be negative
    if (r < 0)
        return -1;
   
    // side of the cube
    float a = (2 * r) / (float)Math.sqrt(3);
    return a;
}
   
// Driver code
public static void main(String args[])
{
    float r = 5;
    System.out.println( largestCube(r));
   
}
 
}
//contributed by Arnab Kundu

Python3

# Python 3 Program to find the biggest
# cube inscribed within a sphere
from math import sqrt
 
# Function to find the side of the cube
def largestCube(r):
     
    # radius cannot be negative
    if (r < 0):
        return -1
 
    # side of the cube
    a = (2 * r) / sqrt(3)
    return a
 
# Driver code
if __name__ == '__main__':
    r = 5
    print("{0:.6}".format(largestCube(r)))
 
# This code is contributed
# by SURENDRA_GANGWAR

C#

// C# Program to find the biggest cube
// inscribed within a sphere
using System;
class Solution{
// Function to find the side of the cube
static float largestCube(float r)
{
 
    // radius cannot be negative
    if (r < 0)
        return -1;
 
    // side of the cube
    float a = (2 * r) / (float)Math.Sqrt(3);
    return a;
}
 
// Driver code
static void Main()
{
    float r = 5;
    Console.WriteLine( largestCube(r));
 
}
 
}
//This code is contributed by mits

PHP

<?php
// PHP Program to find the biggest
// cube inscribed within a sphere
 
// Function to find the side
// of the cube
function largestCube($r)
{
 
    // radius cannot be negative
    if ($r < 0)
        return -1;
 
    // side of the cube
    $a = (float)((2 * $r) / sqrt(3));
    return $a;
}
 
// Driver code
$r = 5;
echo largestCube($r);
 
// This code is contributed by akt_mit
?>

Javascript

<script>
// javascript Program to find the biggest cube
// inscribed within a sphere
 
// Function to find the side of the cube
function largestCube(r)
{
   
    // radius cannot be negative
    if (r < 0)
        return -1;
   
    // side of the cube
    var a = (2 * r) / Math.sqrt(3);
    return a;
}
   
// Driver code 
var r = 5;
document.write( largestCube(r).toFixed(5));
 
// This code is contributed by 29AjayKumar
</script>
Producción: 

5.7735

 

Complejidad de 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

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *