The naive approach to computing
is to simply compute
by repeatedly multiplying by
and
reducing modulo
. Note that after each arithmetic operation is
completed, we reduce the result modulo
so that the sizes of the
numbers involved do not get too large. Nonetheless, this algorithm is
horribly inefficient because it takes
multiplications, which
is huge if
has hundreds of digits.
A much more efficient algorithm for computing
involves
writing
in binary, then expressing
as a product of
expressions
, for various
. These latter expressions can
be computed by repeatedly squaring
. This more clever algorithm
is not ``simpler'', but it is vastly more efficient since the number
of operations needed grows with the number of binary digits of
, whereas
with the naive algorithm above the number of operations is
.
sage: 100.str(2) '1100100'Notice the above is the correct binary expansion:
sage: 0*2^0 + 0*2^1 + 1*2^2 + 0*2^3 + 0*2^4 + 1*2^5 + 1*2^6 100
Thus
We now compute
![]() |
![]() |
|
![]() |
![]() |
|
![]() |
![]() |
|
![]() |
![]() |
Next, compute
![]() |
![]() |
|
![]() |
![]() |
|
![]() |
![]() |
|
![]() |
![]() |
sage: Mod(7,100)^91 43We can also, of course, directly compute
sage: 7^91 80153343160247310515380886994816022539378033762994852007501964604841680190743
William 2007-06-01