Find cubic root of a number - GeeksforGeeks

28 Jun, 2021
Given a number n, find the cube root of n.Examples:  
Input:  n = 3
Output: Cubic Root is 1.442250

Input: n = 8
Output: Cubic Root is 2.000000
 
We can use binary search. First we define error e. Let us say 0.0000001 in our case. The main steps of our algorithm for calculating the cubic root of a number n are:  
Initialize start = 0 and end = nCalculate mid = (start + end)/2Check if the absolute value of (n – mid*mid*mid) < e. If this condition holds true then mid is our answer so return mid.  If (mid*mid*mid)>n then set end=midIf (mid*mid*mid)<n set start=mid.
Initialize start = 0 and end = n
Calculate mid = (start + end)/2
Check if the absolute value of (n – mid*mid*mid) < e. If this condition holds true then mid is our answer so return mid.  
If (mid*mid*mid)>n then set end=mid
If (mid*mid*mid)<n set start=mid.
Below is the implementation of above idea.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find cubic root of a number// using Binary Search#include <bits/stdc++.h>using namespace std; // Returns the absolute value of n-mid*mid*middouble diff(double n,double mid){    if (n > (mid*mid*mid))        return (n-(mid*mid*mid));    else        return ((mid*mid*mid) - n);} // Returns cube root of a no ndouble cubicRoot(double n){    // Set start and end for binary search    double start = 0, end = n;     // Set precision    double e = 0.0000001;     while (true)    {        double mid = (start + end)/2;        double error = diff(n, mid);         // If error is less than e then mid is        // our answer so return mid        if (error <= e)            return mid;         // If mid*mid*mid is greater than n set        // end = mid        if ((mid*mid*mid) > n)            end = mid;         // If mid*mid*mid is less than n set        // start = mid        else            start = mid;    }} // Driver codeint main(){    double n = 3;    printf("Cubic root of %lf is %lf\n",           n, cubicRoot(n));    return 0;}
// Java program to find cubic root of a number// using Binary Searchimport java.io.*; class GFG{    // Returns the absolute value of n-mid*mid*mid    static double diff(double n,double mid)    {        if (n > (mid*mid*mid))            return (n-(mid*mid*mid));        else            return ((mid*mid*mid) - n);    }         // Returns cube root of a no n    static double cubicRoot(double n)    {        // Set start and end for binary search        double start = 0, end = n;          // Set precision        double e = 0.0000001;          while (true)        {            double mid = (start + end)/2;            double error = diff(n, mid);              // If error is less than e then mid is            // our answer so return mid            if (error <= e)                return mid;              // If mid*mid*mid is greater than n set            // end = mid            if ((mid*mid*mid) > n)                end = mid;              // If mid*mid*mid is less than n set            // start = mid            else                start = mid;        }    }         // Driver program to test above function    public static void main (String[] args)    {        double n = 3;        System.out.println("Cube root of "+n+" is "+cubicRoot(n));    }} // This code is contributed by Pramod Kumar
# Python 3 program to find cubic root# of a number using Binary Search # Returns the absolute value of# n-mid*mid*middef diff(n, mid) :    if (n > (mid * mid * mid)) :        return (n - (mid * mid * mid))    else :        return ((mid * mid * mid) - n)         # Returns cube root of a no ndef cubicRoot(n) :         # Set start and end for binary    # search    start = 0    end = n         # Set precision    e = 0.0000001    while (True) :                 mid = (start + end) / 2        error = diff(n, mid)         # If error is less than e        # then mid is our answer        # so return mid        if (error <= e) :            return mid                     # If mid*mid*mid is greater        # than n set end = mid        if ((mid * mid * mid) > n) :            end = mid                     # If mid*mid*mid is less        # than n set start = mid        else :            start = mid             # Driver coden = 3print("Cubic root of", n, "is",      round(cubicRoot(n),6))  # This code is contributed by Nikita Tiwari.
// C# program to find cubic root// of a number using Binary Searchusing System; class GFG {         // Returns the absolute value    // of n - mid * mid * mid    static double diff(double n, double mid)    {        if (n > (mid * mid * mid))            return (n-(mid * mid * mid));        else            return ((mid * mid * mid) - n);    }         // Returns cube root of a no. n    static double cubicRoot(double n)    {                 // Set start and end for        // binary search        double start = 0, end = n;         // Set precision        double e = 0.0000001;         while (true)        {            double mid = (start + end) / 2;            double error = diff(n, mid);             // If error is less than e then            // mid is our answer so return mid            if (error <= e)                return mid;             // If mid * mid * mid is greater            // than n set end = mid            if ((mid * mid * mid) > n)                end = mid;             // If mid*mid*mid is less than            // n set start = mid            else                start = mid;        }    }         // Driver Code    public static void Main ()    {        double n = 3;        Console.Write("Cube root of "+ n                       + " is "+cubicRoot(n));    }} // This code is contributed by nitin mittal.
<?php// PHP program to find cubic root// of a number using Binary Search // Returns the absolute value// of n - mid * mid * midfunction diff($n,$mid){    if ($n > ($mid * $mid * $mid))        return ($n - ($mid *                $mid * $mid));    else        return (($mid * $mid *                 $mid) - $n);} // Returns cube root of a no nfunction cubicRoot($n){         // Set start and end    // for binary search    $start = 0;    $end = $n;     // Set precision    $e = 0.0000001;     while (true)    {        $mid = (($start + $end)/2);        $error = diff($n, $mid);         // If error is less        // than e then mid is        // our answer so return mid        if ($error <= $e)            return $mid;         // If mid*mid*mid is        // greater than n set        // end = mid        if (($mid * $mid * $mid) > $n)            $end = $mid;         // If mid*mid*mid is        // less than n set        // start = mid        else            $start = $mid;    }}     // Driver Code    $n = 3;    echo("Cubic root of $n is ");    echo(cubicRoot($n)); // This code is contributed by nitin mittal.?>
<script> // Javascript program to find cubic root of a number// using Binary Search     // Returns the absolute value of n-mid*mid*mid    function diff(n, mid)    {        if (n > (mid*mid*mid))            return (n-(mid*mid*mid));        else            return ((mid*mid*mid) - n);    }           // Returns cube root of a no n    function cubicRoot(n)    {        // Set start and end for binary search        let start = 0, end = n;            // Set precision        let e = 0.0000001;            while (true)        {            let mid = (start + end)/2;            let error = diff(n, mid);                // If error is less than e then mid is            // our answer so return mid            if (error <= e)                return mid;                // If mid*mid*mid is greater than n set            // end = mid            if ((mid*mid*mid) > n)                end = mid;                // If mid*mid*mid is less than n set            // start = mid            else                start = mid;        }    }    // Driver Code             let n = 3;        document.write("Cube root of "+n+" is "+cubicRoot(n));         </script>
Output: 
Cubic root of 3.000000 is 1.442250
Time Complexity : O(Log n)This article is contributed by Madhur Modi .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
splevel62
Binary Search
Divide and Conquer
Mathematical
Mathematical
Divide and Conquer
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count number of occurrences (or frequency) in a sorted array
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Quick Sort vs Merge Sort
Closest Pair of Points using Divide and Conquer algorithm
Find a peak element
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7