python - pow 或 ** 用于 Python 中的非常大的数字

标签 python python-2.7 math numpy

我正在尝试用 Python 计算一些 num1**num2。但问题是 num193192289535368032Lnum284585482668812077L,它们都是非常大的数字。

我尝试了以下几种方法:首先,我尝试使用**运算符来计算它。但是时间太长了(我等了大概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/

相关文章:

python - 这些 for 循环可以重构吗?

python - 如何将 XML 放入 HTML 模板中? python /Django

python - 将变量分配给Python对象

c - 为什么这个 C 表达式会产生错误的答案?

javascript - 将圆内的点位置表示为任意线段的百分比

python - 为什么我不能在 PyCharm 中导入两个模块?

python - 用户定义的通用类型和 collections.abc

python - 在 QListWidgets 之间拖放自定义小部件项目

python - 获取作为参数传递的内置函数的方法名称并在函数体内调用

math - 增强现实演示