我正在尝试用 Python 计算一些 num1**num2
。但问题是 num1
是 93192289535368032L
而 num2
是 84585482668812077L
,它们都是非常大的数字。
我尝试了以下几种方法:首先,我尝试使用**
运算符来计算它。但是时间太长了(我等了大概2个小时,没有结果)。
其次,我使用了 math.pow(num1, num2)
。但我得到了这个:
Traceback (most recent call last): File "<pyshell#23>", line 1, in <module>
math.pow(84585482668812077L, 93192289535368032L)
OverflowError: math range error
最后,我使用了numpy.power
:
numpy.power(84585482668812077, 93192289535368032)
-9223372036854775808
如你所见,它给了我负数。
我真正想做的是 result = (num1**num2)
然后是 result % num3
。所以,我需要有效地计算这个功率值。
我该怎么做?
最佳答案
您应该将 num3 作为第三个参数传递给 pow
pow(...) pow(x, y[, z]) -> number With two arguments, equivalent to x**y. With three arguments, equivalent to (x**y) % z, but may be more efficient (e.g. for longs).
关于python - pow 或 ** 用于 Python 中的非常大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23759098/