Saturday, January 11, 2025
Google search engine
HomeData Modelling & AILargest sphere that can be inscribed within a cube which is in...

Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone

Given here is a right circular cone of radius r and perpendicular height h, which is inscribed in a cube which in turn is inscribed in a sphere, the task is to find the radius of the sphere.
Examples: 
 

Input: h = 5, r = 6 
Output: 1.57306

Input: h = 8, r = 11
Output: 2.64156

 

 

Approach
 

Below is the implementation of the above approach:
 

C++




// C++ Program to find the biggest sphere
// which is inscribed within a cube which in turn
// inscribed within a right circular cone
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the radius of the sphere
float sphereSide(float h, float r)
{
    // height and radius cannot be negative
    if (h < 0 && r < 0)
        return -1;
 
    // radius of the sphere
    float R = ((h * r * sqrt(2)) / (h + sqrt(2) * r)) / 2;
 
    return R;
}
 
// Driver code
int main()
{
    float h = 5, r = 6;
 
    cout << sphereSide(h, r) << endl;
 
    return 0;
}


Java




// Java Program to find the biggest sphere
// which is inscribed within a cube which in turn
// inscribed within a right circular cone
import java.lang.Math;
 
class GFG
{
     
// Function to find the radius of the sphere
static float sphereSide(float h, float r)
{
    // height and radius cannot be negative
    if (h < 0 && r < 0)
        return -1;
 
    // radius of the sphere
    float R = (float)((h * r * Math.sqrt(2)) /
                    (h + Math.sqrt(2) * r)) / 2;
 
    return R;
}
 
// Driver code
public static void main(String[] args)
{
    float h = 5, r = 6;
 
    System.out.println(sphereSide(h, r));
 
}
}
 
// This code is contributed by Code_Mech.


Python3




# Program to find the biggest sphere
# which is inscribed within a cube which in turn
# inscribed within a right circular cone
import math
 
# Function to find the radius of the sphere
def sphereSide(h, r):
 
    # height and radius cannot be negative
    if h < 0 and r < 0:
        return -1
 
    # radius of the sphere
    R = (((h * r * math.sqrt(2))) /
              (h + math.sqrt(2) * r) / 2)
 
    return R
 
# Driver code
h = 5; r = 6
print(sphereSide(h, r))
 
# This code is contributed by Shrikant13


C#




// C# Program to find the biggest sphere
// which is inscribed within a cube which in turn
// inscribed within a right circular cone
using System;
 
class GFG
{
     
// Function to find the radius of the sphere
static float sphereSide(float h, float r)
{
    // height and radius cannot be negative
    if (h < 0 && r < 0)
        return -1;
 
    // radius of the sphere
    float R = (float)((h * r * Math.Sqrt(2)) /
                      (h + Math.Sqrt(2) * r)) / 2;
 
    return R;
}
 
// Driver code
public static void Main()
{
    float h = 5, r = 6;
 
    Console.WriteLine(sphereSide(h, r));
}
}
 
// This code is contributed by Code_Mech


PHP




<?php
// PHP Program to find the biggest sphere
// which is inscribed within a cube which in turn
// inscribed within a right circular cone
 
// Function to find the radius of the sphere
function sphereSide($h, $r)
{
    // height and radius cannot be negative
    if ($h < 0 && $r < 0)
        return -1;
 
    // radius of the sphere
    $R = (($h * $r * sqrt(2)) /
          ($h + sqrt(2) * $r)) / 2;
 
    return $R;
}
 
// Driver code
$h = 5; $r = 6;
 
echo(sphereSide($h, $r));
 
// This code is contributed by Code_Mech.
?>


Javascript




<script>
 
// javascript Program to find the biggest sphere
// which is inscribed within a cube which in turn
// inscribed within a right circular cone
 
// Function to find the radius of the sphere
function sphereSide(h , r)
{
    // height and radius cannot be negative
    if (h < 0 && r < 0)
        return -1;
 
    // radius of the sphere
    var R = ((h * r * Math.sqrt(2)) /
                    (h + Math.sqrt(2) * r)) / 2;
 
    return R;
}
 
// Driver code
var h = 5, r = 6;
 
document.write(sphereSide(h, r).toFixed(5));
 
 
// This code is contributed by Amit Katiyar
 
</script>


Output

1.57306

Time Complexity: O(1)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
05 Aug, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

Most Popular

Recent Comments