Information AboutExponentiation By Squaring |
| CATEGORIES ABOUT EXPONENTIATION BY SQUARING | |
| exponentials | |
| arbitrary precision algorithms | |
| computer arithmetic | |
|
SQUARING ALGORITHM The following Recursive Algorithm computes ''x''''n'' for a Positive Integer ''n'': : Compared to the ordinary method of multiplying ''x'' with itself ''n'' − 1 times, this algorithm uses only O (log ''n'') multiplications and therefore speeds up the computation of ''x''''n'' tremendously, in much the same way that the "long multiplication" algorithm speeds up multiplication over the slower method of repeated addition. The benefit is had for ''n'' greater than or equal to ''4''. FURTHER APPLICATIONS 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 :Power(''x'', -''n'') = (Power(''x'', ''n''))-1. The method works in every Semigroup and is often used to compute powers of Matrices , For example, the evaluation of :13789722341 (mod 2345) 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 x
Some more examples:
EXAMPLE IMPLEMENTATIONS Computation by powers of 2 This is a non-recursive implementation of the above algorithm in the Ruby Programming Language . In most Statically Typed languages, result=1 must be replaced 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. def power(x,n)
n = n-1 else
n = n/2 end end return result end Runtime example: Compute 310 parameter x = 3 parameter n = 10 result := 1 Iteration 1 n = 10 -> n is even x := x2 = 32 = 9 n := n / 2 = 5 Iteration 2 n = 5 -> n is odd
n := n - 1 = 4 x := x2 = 92 = 34 = 81 n := n / 2 = 2 Iteration 3 n = 2 -> n is even x := x2 = 812 = 38 = 6,561 n := n / 2 = 1 Iteration 4 n = 1 -> n is odd
n := n - 1 = 0 return result Computation by binary representation function power ( x, n )
end return result end Runtime example: Compute 310 result := 3 Bin := "1010" Iteration for digit 2: result := result2 = 32 = 9 1010bin - Digit equals "0" Iteration for digit 3: result := result2 = (32)2 = 34 = 81
Iteration for digit 4:
1010bin - Digit equals "0" return result JavaScript-Demonstration: http://home.arcor.de/wzwz.de/wiki/ebs/en.htm Generalization with example Generalization
For all elements a of S:
Now the algorithm exponentiation by squaring may be used for fast computing of E-values. Text application Because the concatenation + is an associative operation on the set of all finite Strings over a fixed alphabet ( with the empty string "" as its Identity Element ) exponentiation by squaring may be used for fast repeating of strings. Example ( javascript ): function repeat ( s, n ) { |
|
|