HomeSoftware DevelopmentPython Program for Discover cubic root of a quantity

# Python Program for Discover cubic root of a quantity

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 will 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. Examine if absolutely the worth of (n â€“ mid*mid*mid) Â
4. If (mid*mid*mid)>n then set finish=mid
5. If (mid*mid*mid)

Under 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 Area: O(1)

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

RELATED ARTICLES