why do I get this error message, when I’m using these parameters(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.
pow():
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
....:
The modular exponentiation by squaring algorithm you implemented should use integers.
e = e/2returns a Rational3/2.You should convert this to an integer
e = (e/2).floor().