VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-cubic-root-of-a-number/

⇱ Find cubic root of a number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find cubic root of a number

Last Updated : 1 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


 

Recommended Practice


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: 
 

  1. Initialize start = 0 and end = n
  2. Calculate mid = (start + end)/2
  3. Check if the absolute value of (n - mid*mid*mid) < e. If this condition holds true then mid is our answer so return mid. 
     
  4. If (mid*mid*mid)>n then set end=mid
  5. If (mid*mid*mid)<n set start=mid.


Below is the implementation of above idea. 
 

Output: 

Cubic root of 3.000000 is 1.442250


Time Complexity: O(logn)

Auxiliary Space: O(1)
 

Comment
Article Tags: