It’s simple: pow
takes an optional 3rd argument for the modulus.
From the docs:
pow(x, y[, z])
Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than
pow(x, y) % z)
. The
two-argument form pow(x, y) is equivalent to using the power operator:
x**y
.
So you want:
pow(6, 8, 5)
Not only is pow(x, y, z)
faster & more efficient than (x ** y) % z
it can easily handle large values of y
without using arbitrary precision arithmetic, assuming z
is a simple machine integer.