Find cubic root of a number
Last Updated :
01 Sep, 2022
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 = 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++
// 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*mid
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
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
int main()
{
double n = 3;
printf("Cubic root of %lf is %lf\n",
n, cubicRoot(n));
return 0;
}
Java
// Java program to find cubic root of a number
// using Binary Search
import 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
Python3
# Python 3 program to find cubic root
# of a number using Binary Search
# Returns the absolute value of
# n-mid*mid*mid
def 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
def 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
print("Cubic root of", n, "is",
round(cubicRoot(n),6))
# This code is contributed by Nikita Tiwari.
C#
// C# program to find cubic root
// of a number using Binary Search
using 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
// PHP 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
$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.
?>
JavaScript
<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(logn)
Auxiliary Space: O(1)
Similar Reads
Fifth root of a number Given a number, print floor of 5'th root of the number.Examples: Input : n = 32 Output : 2 2 raise to power 5 is 32 Input : n = 250 Output : 3 Fifth square root of 250 is between 3 and 4 So floor value is 3. Method 1 (Simple) A simple solution is initialize result as 0, keep incrementing result whil
10 min read
N-th root of a number Given two numbers n and m, find the n-th root of m. In mathematics, the n-th root of a number m is a real number that, when raised to the power of n, gives m. If no such real number exists, return -1.Examples: Input: n = 2, m = 9Output: 3Explanation: 32 = 9Input: n = 3, m = 9Output: -1Explanation: 3
9 min read
Square root of a number using log For a given number find the square root using log function. Number may be int, float or double. Examples: Input : n = 9Output : 3 Input : n = 2.93Output : 1.711724 We can find square root of a number using sqrt() method: C++ // C++ program to demonstrate finding // square root of a number using sqrt
3 min read
Digital Root of a given large number using Recursion Given a large number num in the form of string with length as N, the task is to find its digital root. The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains t
7 min read
Program to find root of an equations using secant method The secant method is used to find the root of an equation f(x) = 0. It is started from two distinct estimates x1 and x2 for the root. It is an iterative procedure involving linear interpolation to a root. The iteration stops if the difference between two intermediate values is less than the converge
8 min read