| |||||||||
Exponentiating by squaring is an algorithm used for the fast computation of large powers of a number x. It is also known as the square-and-multiply algorithm or binary exponentation. It implicitly uses the binary expansion of the exponent. It is of quite general use, for example in modular arithmetic.
The following recursive algorithm computes xn for a positive integer n:
\mbox{Power}(x,n)=\left\{ \begin{matrix} x, & \mbox{if }n\mbox{ = 1} \\ \mbox{Power}(x^2, n/2), & \mbox{if }n\mbox{ is even} \\ x*\mbox{Power}(x^2, (n-1)/2), & \mbox{if }n >\mbox{2 is odd} \\ \end{matrix}\right. <math>
Compared to the ordinary method of multiplying x with itself n − 1 times, this algorithm uses only O(lg n) multiplications and therefore speeds up the computation of xn tremendously, in much the same way that the "long multiplication" algorithm speeds up multiplication over the slower method of repeated addition.
The same idea allows fast computation of large exponents modulo a number. Especially in cryptography, it is useful to compute powers in a ring of integers modulo q. It can also be used to compute integer powers in a group, using the rule
The method works in every semigroup and is often used to compute powers of matrices,
For example, the evaluation of
would take a very long time and lots of storage space if the naïve method is used: compute 13789722341 then take the remainder when divided by 2345. Even using a more effective method will take a long time: square 13789, take the remainder when divided by 2345, multiply the result by 13789, and so on. This will take 722340 modular multiplications. The square-and-multiply algorithm is based on the observation that 13789722341 = 13789(137892)361170. So, if we computed 137892, then the full computation would only take 361170 modular multiplications. This is a gain of a factor of two. But since the new problem is of the same type, we can apply the same observation again, once more approximately halving the size.
The repeated application of this algorithm is equivalent to decomposing the exponent (by a base conversion to binary) into a sequence of squares and products: for example
where 7 = (111)2 = 22+21+20
This is an implementation of the above algorithm in the Ruby programming language. It doesn't use recursion, which increases the speed even further.
In most languages you'll need to replace result=1 with code assigning an identity matrix of the same size as x to result to get a matrix exponentiating algorithm. In Ruby, thanks to coercion, result is automatically upgraded to the appropriate type, so this function works with matrices as well as with integers and floats.
Addition chain exponentiation can in some cases require fewer multiplications by using an efficient addition chain to provide the multiplication order. However, exponentiating by squaring is simpler to set up and typically requires less memory.