community
directory
books
authors
images
encyclopedia

Email:
Password:
Register

Knowledgerush Search

 

Google
  Web knowledgerush


Search for images of Exponentiating by squaring


Message boards   Post comment

Exponentiating by squaring

Exponentiating by squaring is an algorithm used for the fast computation of large powers of a number x. 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(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 method works in every semigroup and is often used to compute powers of matrices, and, especially in cryptography, 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.

Essentially, this algorithm is equivalent to decomposing the exponent (often means by a base conversion to binary) into a sequence of squares and products: for example

x7=
=x4x2x1
=(x2)2x2x
where 7=(110)2=22+2+20

Example implementation

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 result=unit_matrix_of_the_same_size_as_x 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
        if ((n % 2) == 1) then
            result = result * x
        end
        x = x*x
        n = n/2
    end
    return result
end

Referenced By

List of algorithms | List of computability and complexity topics | List of group theory topics | List of mathematical topics (D-F) | List of mathematical topics (F-Z) | Ruby language | Ruby programming language

 

Compose Your Message

Your Email Address or Pen Name (optional):
Subject:
Your Message:
 

 

 

 

 

 

This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Exponentiating by squaring".

 

Contact UsPrivacy Statement & Terms of Use

 
Copyright © 1999-2003 Knowledgerush.com. All rights reserved.