Goo*_*dGJ 9 python math numpy python-2.7
我试图num1**num2
在Python中计算一些.但问题是,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
Run Code Online (Sandbox Code Playgroud)
最后,我使用了numpy.power
:
numpy.power(84585482668812077, 93192289535368032)
-9223372036854775808
Run Code Online (Sandbox Code Playgroud)
如你所见,它给了我减去.
我真正想做的就是result = (num1**num2)
然后result % num3
.所以,我需要有效地计算这个功率值.
我怎样才能做到这一点?
Joh*_*ooy 15
您应该将num3作为第3个参数传递给 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).