Given a number n, find the cube root of n.
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.
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 contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
- K-th Element of Two Sorted Arrays
- Allocate minimum number of pages
- Easy way to remember Strassen’s Matrix Equation
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Floor in a Sorted Array
- Find frequency of each element in a limited range array in less than O(n) time
- Longest Common Prefix using Divide and Conquer Algorithm
- Square root of an integer
- Find the element that appears once in a sorted array
- Euclidean algorithms (Basic and Extended)
- Tiling Problem using Divide and Conquer algorithm
- Replace all ‘0’ with ‘5’ in an input Integer
- Binary Search
- Divide and Conquer Algorithm | Introduction
- Write a program to calculate pow(x,n)
Improved By : nitin mittal