How to find integer nth roots?

后端 未结 11 1498
醉酒成梦
醉酒成梦 2020-12-01 16:22

I want to find the greatest integer less than or equal to the kth root of n. I tried

int(n**(1/k))

But for n=125, k=3 this gives the wrong

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 16:50

    Why not to try this :

    125 ** (1 / float(3)) 
    

    or

    pow(125, 1 / float(3))
    

    It returns 5.0, so you can use int(), to convert to int.

提交回复
热议问题