0

为什么我在使用这些参数时会收到此错误消息(pow(1,3,3))?:

sage: pow(1,3,3)                                    
3
3/2
3/2
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)

/home/kai/<ipython console> in <module>()

/home/kai/<ipython console> in pow(a, e, n)

/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/rings/rational.so in sage.rings.rational.Rational.__mod__ (sage/rings/rational.c:19891)()

/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/rings/integer.so in sage.rings.integer.Integer.inverse_mod (sage/rings/integer.c:32726)()

ZeroDivisionError: Inverse does not exist.

战俘():

def pow(a,e,n):
....:     num = 1
....:     while e >= 1:
....:         print(e)
....:         if (e%2) == 1:
....:             num = (num*a) % n  
....:         e = e/2
....:         print(e)
....:         a = (a*a) % n
....:     return num
....:
4

2 回答 2

3

您实现的模幂平方算法应该使用整数。

e = e/2返回一个 Rational 3/2

您应该将其转换为整数e = (e/2).floor()

于 2012-12-18T14:26:29.380 回答
-1
a = (a*a) % n

a = 1*1 % 1 a -> 0 这意味着你的情况是 0,所以在这里

num = (num*a) % n 

你将有 0 % 3。

编辑:可以在 python 中做 3/2 % 3 吗?

于 2012-12-18T14:20:05.497 回答