我想在Python 3中计算一个非常大的数字的立方根。
我尝试了下面的函数,以及Python语法x ** (1 / n)
,但它们都产生了一个错误:
OverflowError:(34,'数值结果超出范围')
我真的需要计算一下立方根来解决密码学中的一个问题。
二分查找:
def find_invpow(x,n):
"""Finds the integer component of the n'th root of x,
an integer such that y ** n <= x < (y + 1) ** n.
"""
high = 1
while high ** n < x:
high *= 2
low = high/2
while low < high:
mid = (low + high) // 2
if low < mid and mid**n < x:
low = mid
elif high > mid and mid**n > x:
high = mid
else:
return mid
return mid + 1
字符串
编号:
2402723012420288965061445118057886492024743551154629917030119156891142918839022160395719857240175320389341288681749366145145967978475131123226312522685251430708710073129238467242975159749008032321188589120967227479657671719747542846097161210054623906120260301055001127882387713498723567963440422423914882959001801987608169316360033781939732394099020551125663699345546906894669213059766588861651631700807482565194653978237220624343007162119668966610411816483339553933008437525708171932730677159310906443685072653956857962468259193402008679184817845439554701795917678112318967148892417504752971110662951416459507065969183113675073168805647217161265168559090283169303349989764721815853152625319121281629196477157281162821124326395105731219640206524664156427517206350452758237590539110470955306030593745489038645874942237826327101675102728158117033520522948576476067569350309419081936561261029262542325027092539027418272029737267
型
2条答案
按热度按时间gorkyyrv1#
您可以使用
decimal
模块进行精确运算。字符串
qvtsj1bj2#
sympy有一个
integer_nthroot
函数,它看起来对你的数字来说足够快:字符串
integer_nthroot
的结果是一对(整数,布尔值);整数是整数的n次方根,布尔值告诉你它是否是精确解;也就是说,你输入的数字是否是完美的n次方。