FACTOID # 157: People trust Swedes! Swedish companies are the world’s least-likely to be perceived as paying bribes.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Binary exponentiation

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 exponentiation. It implicitly uses the binary expansion of the exponent. It is of quite general use, for example in modular arithmetic.

Contents

Squaring algorithm

The following recursive algorithm computes xn 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 xn tremendously, in much the same way that the "long multiplication" algorithm speeds up multiplication over the slower method of repeated addition.


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

x7
= x4*x2*x1
= (x2)2*x2*x
= (x2*x)2*x       → algorithm needs only 4 multiplications instead of 7 - 1 = 6

where 7 = (111)2 = 22+21+20


Some more examples:

  • x10 = ((x2)2*x)2 because 10 = (1,010)2 = 23+21 , algorithm needs 4 multiplications instead of 9
  • x100 = (((((x2*x)2)2)2*x)2)2 because 100 = (1,100,100)2 = 26+25+22, algorithm needs 8 multiplications instead of 99
  • x1,000 = ((((((((x2*x)2*x)2*x)2*x)2)2* x)2)2)2 because 103 = (1,111,101,000)2, algorithm needs 14 multiplications instead of 999
  • x1,000,000 = ((((((((((((((((((x2*x)2*x)2*x)2)2*x) 2)2)2)2)2*x)2)2)2*x)2) 2)2)2)2)2 because 106 = (11,110,100,001,001,000,000)2, algorithm needs 25 multiplications
  • x1,000,000,000 = ((((((((((((((((((((((((((((x2*x)2*x)2)2*x)2* x)2*x)2)2)2*x)2*x)2)2*x) 2)2*x)2*x)2)2)2*x)2)2*x) 2)2)2)2)2)2)2)2)2 because 109 = (111,011,100,110,101,100,101,000,000,000)2, algorithm needs 41 multiplications

Example implementations

Computation by powers of 2

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.

 def power(x,n) result = 1 while (n != 0) # if n is odd, multiply result with x. decrement n by 1 if (n.modulo(2) == 1) then result = result * x n = n_1 end # last iteration: no need to compute x = one more power of 2 if (n > 0) then x = x*x 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 _> result := result * x = 1 * x = 1 * 32 = 9 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 _> result := result * x = 32 * 38 = 310 = 9 * 6561 = 59,049 n := n _ 1 = 0 return result 

Computation by binary representation

 function power ( x , n ) if ( n equals 0 ) return 1 result := x bin := binary-representation-of ( n ) for digit := second-digit-of-bin to last-digit-of-bin result := result * result if ( digit equals "1" ) result := result * x 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 1010bin _ Digit equals "1" __> result := result*3 = (32)2*3 = 35 = 243 
 Iteration for digit 4: result := result2 = ((32)2*3)2 = 310 = 59,049 1010bin _ Digit equals "0" 
 return result 

JavaScript_Demonstration: http://home.arcor.de/wzwz.de/wiki/ebs/en.htm


Generalization with example

Generalization

Let the pair ( S , * ) be a Semigroup, that means S is an arbitrary set and * is an associative binary operation on S:

  • For all elements a and b of S is a*b also an element of S
  • For all elements a, b and c of S is valid: (a*b)*c equals a*(b*c)

We may call * a "multiplication" and define an "exponentiation" E in the following way:
For all elements a of S:

  • E ( a , 1 ) := a
  • For all natural numbers n > 0 is defined: E ( a , n+1 ) := E ( a , n ) * a

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 ) { if ( s == "" || n < 1 ) return "" var res = s var bin = n.toString ( 2 ) for ( var i = 1 ; i < bin.length ; i++ ) { res = res + res if ( bin.charAt ( i ) == '1' ) res = res + s } return res } The call repeat ( 'Abc' , 6 ) returns the string AbcAbcAbcAbcAbcAbc 

Alternatives

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.






  Results from FactBites:
 
Binary operation - Wikipedia, the free encyclopedia (399 words)
In mathematics, a binary operation, or binary operator, is a calculation involving two input quantities and one kind of a specific operation.
Binary operations are the keystone of algebraic structures studied in abstract algebra: they form part of groups, monoids, semigroups, rings, and more.
Binary operations are often written using infix notation such as a * b, a + b, or a · b rather than by functional notation of the form f(a,b).
Exponentiation - encyclopedia article about Exponentiation. (3649 words)
Exponentiation can also be understood purely in terms of abstract algebra, if we limit the exponents to integers.
Exponential notation is also used, especially in group theory, to indicate conjugation.
Note that exponentiation of cardinal numbers doesn't match up with exponentiation of ordinal numbers, which is defined by a limit process.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m