Monday, October 31, 2022
HomeWordPress DevelopmentPython Program for Discover cubic root of a quantity

Python Program for Discover cubic root of a quantity


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given a quantity n, discover the dice root of n.
Examples: 
 

Enter:  n = 3
Output: Cubic Root is 1.442250

Enter: n = 8
Output: Cubic Root is 2.000000

 

We are able to use binary search. First we outline error e. Allow us to say 0.0000001 in our case. The principle steps of our algorithm for calculating the cubic root of a quantity n are: 
 

  1. Initialize begin = 0 and finish = n
  2. Calculate mid = (begin + finish)/2
  3. Verify if absolutely the worth of (n – mid*mid*mid)  
  4. If (mid*mid*mid)>n then set finish=mid
  5. If (mid*mid*mid)

Beneath is the implementation of above concept. 
 

Python3

  

def diff(n, mid) :

    if (n > (mid * mid * mid)) :

        return (n - (mid * mid * mid))

    else :

        return ((mid * mid * mid) - n)

          

def cubicRoot(n) :

      

    

    

    begin = 0

    finish = n

      

    

    e = 0.0000001

    whereas (True) :

          

        mid = (begin + finish) / 2

        error = diff(n, mid)

  

        

        

        

        if (error <= e) :

            return mid

              

        

        

        if ((mid * mid * mid) > n) :

            finish = mid

              

        

        

        else :

            begin = mid

              

n = 3

print("Cubic root of", n, "is"

      spherical(cubicRoot(n),6))

Output: 

Cubic root of three.000000 is 1.442250

Time Complexity: O(logn)

Auxiliary House: O(1)

Please refer full article on Discover cubic root of a quantity for extra particulars!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments