如何在Python中获取多维数据集根目录

Mas*_*tla 2 python

我最近看过这篇文章,关于在python中获取平方根:如何在python 中计算平方根?

我已经使用了该sqrt(x)命令,但我不知道如何使它适用于立方体根和任何高于此的东西.

另外,我使用的是Python 3.5.2.

对此有何帮助?

Moi*_*dri 5

在Python中,您可能会发现浮动cube root:

>>> def get_cube_root(num):
...     return num ** (1. / 3)
... 
>>> get_cube_root(27)
3.0
Run Code Online (Sandbox Code Playgroud)

如果您想要更通用的查找方法nth root,可以使用以下基于以下代码的代码Newton's method:

# NOTE: You may use this function only for integer numbers
# k is num, n is the 'n' in nth root
def iroot(k, n):
    u, s = n, n+1
    while u < s:
        s = u
        t = (k-1) * s + n // pow(s, k-1)
        u = t // k
    return s
Run Code Online (Sandbox Code Playgroud)

  • 看看在什么情况下你的`iroot`优于`k**(1./n)`会很有趣. (2认同)