+ 13
How to take the cube root of a number in python using exponentiation ?
I want to make a calculator code which has the option of cube root. I know how to take the square root of a number. but what about cube roots ? any help would be appreciated. thank you!
20 ответов
+ 10
Just use the inverse of the power raised.
N**(1.0/3.0)
note, this only works for positive values of N.
+ 17
You can use
pow(x, y)
x - the number whose root you want to find
y - the degree to which to raise the number
If y = 1/3 then you get the cubic root
pow(27,1/3)
+ 10
Thanks a lot Михаил Горчанюк and Robert Atkins
+ 9
Thanks a lot Paolo De Nictolis and Ankit Raghuvanshi
+ 9
+ 9
Newton-Raphson only needs 8 iterations for sqrt(27) (even when you start with a very poor initial estimate at 1):
c = 0
n = 27
y = 1
while abs(y**3 - n) > 0.00001:
y = y - (y**3-n)/(3*y**2)
c+= 1
print("iterations: ", c)
print(round(y))
+ 9
thank you so much Thoq!
+ 9
I will do that Thoq! and thanks a lot for recommending
+ 8
Thanks Utkarsh Pratap Maurya
+ 8
thank you so much Egbeola Samuel ☺
+ 8
Thoq!, i just wanted to know the different methods of getting cube roots.
check out this code and please tell me if i can add anything else
https://code.sololearn.com/cQ2Wc082SeBM/?ref=app
+ 4
sujay simha I know that this is not really what you were looking for. But I still found it might be interesting to show how a (simple) implementation for finding a root might look like.
+ 4
sujay simha When taking a look at my calculator, I can see a lot of stuff like sine, cosine, log, tan, mean... which you could also implement.
+ 3
like this
4***3
+ 3
pow(a,b)
b=1/3 while
a= The number you want to find the cube root of
+ 3
You are always welcome with more problems
+ 2
Import math as m
a, b=int(input("enter a number ")), int(input("enter power"))
s=m.pow(a, b)
print(s)
+ 1
pow(num, 1/3)
+ 1
thnx
0
This has solved a lot of my problems