Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
Last Updated :
29 Feb, 2024
Given a cube of side length a, which inscribes a sphere which in turn inscribes a right circular cone. The task is to find the largest possible volume of this cone.
Examples:Â
Â
Input: a = 5
Output: 58.1481
Input: a = 8
Output: 238.175
Â
Approach:Â
Let, the height of right circular cone = h.Â
Radius of the cone = rÂ
Radius of the sphere = RÂ
We, know radius of the sphere inside the cube, r = a/2. Please refer ( Largest sphere that can be inscribed inside a cube).Â
Also, height of cone inside the sphere, h = 4r/3.Â
radius of cone inside the sphere, r = 2√2r/3. Please refer (Largest right circular cone that can be inscribed within a sphere).Â
So, height of the cone inside the sphere which in turn is inscribed within a cube, h = 2a/3.Â
Radius of the cone inside the sphere which in turn is inscribed within a cube, r = √2a/3.
Below is the implementation of the above approach:Â
Â
C++
#include <bits/stdc++.h>
using namespace std;
float cone( float a)
{
if (a < 0)
return -1;
float r = (a * sqrt (2)) / 3;
float h = (2 * a) / 3;
float V = 3.14 * pow (r, 2) * h;
return V;
}
int main()
{
float a = 5;
cout << cone(a) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static float cone( float a)
{
if (a < 0 )
return - 1 ;
float r = ( float ) (a * Math.sqrt( 2 )) / 3 ;
float h = ( 2 * a) / 3 ;
float V = ( float )( 3.14 *Math. pow(r, 2 ) * h);
return V;
}
public static void main (String[] args)
{
float a = 5 ;
System.out.println( cone(a));
}
}
|
Python3
import math
def cone(a):
if (a < 0 ):
return - 1 ;
r = (a * math.sqrt( 2 )) / 3 ;
h = ( 2 * a) / 3 ;
V = 3.14 * math. pow (r, 2 ) * h;
return V;
a = 5 ;
print (cone(a));
|
C#
using System;
class GFG
{
static double cone( double a)
{
if (a < 0)
return -1;
double r = ( double ) (a * Math.Sqrt(2)) / 3;
double h = (2 * a) / 3;
double V = ( double )(3.14 * Math.Pow(r, 2) * h);
return Math.Round(V,4);
}
static void Main ()
{
double a = 5;
Console.WriteLine(cone(a));
}
}
|
Javascript
<script>
function cone(a)
{
if (a < 0)
return -1;
var r = (a * Math.sqrt(2)) / 3;
var h = (2 * a) / 3;
var V = (3.14 *Math. pow(r, 2) * h);
return V;
}
var a = 5;
document.write( cone(a).toFixed(5));
</script>
|
PHP
<?php
function cone( $a )
{
if ( $a < 0)
return -1;
$r = ( $a * sqrt(2)) / 3;
$h = (2 * $a ) / 3;
$V = 3.14 * pow( $r , 2) * $h ;
return $V ;
}
$a = 5;
echo round (cone( $a ), 4);
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...