Cube Inscribed In A Sphere
Largest cube that can be inscribed inside the sphere
Given here is a sphere of radius r, the task is to find the side of the largest cube that tin can fit inside in it.
Examples:
Input: r = 8 Output: ix.2376 Input: r = v Output: v.7735
Arroyo:
Side of the cube = a
Radius of the sphere = r
From the diagonal, information technology is articulate that, diagonal of the cube = diameter of the sphere,
a√3 = 2r or, a = 2r/√3
Below is the implementation:
C++
#include <$.25/stdc++.h>
using namespace std;
float largestCube( bladder r)
{
if (r < 0)
return -1;
float a = (2 * r) / sqrt (iii);
return a;
}
int main()
{
bladder r = five;
cout << largestCube(r) << endl;
render 0;
}
Coffee
import java.util.*;
class Solution{
static float largestCube( bladder r)
{
if (r < 0 )
return - 1 ;
float a = ( 2 * r) / ( float )Math.sqrt( 3 );
return a;
}
public static void master(String args[])
{
float r = 5 ;
System.out.println( largestCube(r));
}
}
Python3
from math import sqrt
def largestCube(r):
if (r < 0 ):
return - 1
a = ( 2 * r) / sqrt( 3 )
return a
if __name__ = = '__main__' :
r = five
print ( "{0:.6}" . format (largestCube(r)))
C#
using System;
grade Solution{
static float largestCube( bladder r)
{
if (r < 0)
render -1;
float a = (2 * r) / ( float )Math.Sqrt(3);
render a;
}
static void Chief()
{
bladder r = 5;
Console.WriteLine( largestCube(r));
}
}
PHP
<?php
function largestCube( $r )
{
if ( $r < 0)
return -one;
$a = (float)((2 * $r ) / sqrt(3));
render $a ;
}
$r = five;
repeat largestCube( $r );
?>
Javascript
<script>
function largestCube(r)
{
if (r < 0)
return -1;
var a = (two * r) / Math.sqrt(three);
return a;
}
var r = v;
document.write( largestCube(r).toFixed(v));
</script>
Time Complexity: O(i)
Auxiliary Space: O(1)
Cube Inscribed In A Sphere,
Source: https://www.geeksforgeeks.org/largest-cube-that-can-be-inscribed-within-the-sphere/
Posted by: boltonjunashe.blogspot.com

0 Response to "Cube Inscribed In A Sphere"
Post a Comment