Fast Exponentiation

The fast algorithm of exponentiation is used to quickly calculate great powers of a number given X . It is also called " method Public garden and multiply" (to put squared and to multiply).

Mathematical writing

The first way of calculating a power n^p, is to multiply N by itself p time. However, there exist methods much more effective, where the number of operations necessary is not any more about p but about log (p) , for the Logarithme in 2 bases. One can for example write p= \ sum_ {I \ Leq D} a_i2^i for a_i \ in \ {0,1 \} , and one notes whereas:
n^p=n^ {a_0} (n^2) ^ {a_1} (n^4) ^ {a_2} \ dowries (n^ {2^d}) ^ {a_d}
One needs D operations thus to calculate all the n^ {2^i} , then D operations additional to form the product of the (n^ {2^i}) ^ {a_i} for I variable. The total number of operations is thus 2d , which is well about the logarithm of p . This simple algebraic remark leads for example to the algorithm below.

Algorithm

That is to say N an entirety strictly higher than 1, let us suppose that one can calculate for any X pertaining to the whole of realities, all the powers xk of X , for all K , such as 1≤ K < N .
  • If N is even then X n= ( X 2) N /2 . It is then enough to calculate there N /2 with y=x2.
  • If N is odd and N >1, then X n= X × ( X 2) ( N -1) /2. It is enough to calculate there ( N -1) /2 with y=x2 and to multiply by X the result.

This remark brings us to the recursive algorithm according to which calculates X N for a strictly positive entirety 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 \ times \ mbox {power} (x^2, \, (n-1) /2), & \ mbox {if} N > \ mbox {2 is odd} \ \ \end{matrix}\right.

While comparing with the ordinary method which consists in multiplying X by itself N -1 times, this algorithm requires about O (lg N ) multiplications and thus accelerates the calculation of X N in a spectacular way for the great entireties.

The method functions in all semigroup and is often used to calculate powers of matrix S, and particularly in Cryptographie, but also to calculate the powers in a ring of entireties modulo Q . It can be also used to calculate powers of an element in a group, by using for the negative powers the rule: power ( X , - N ) = (power ( X , N ))-1.

Examples of implementation

Here an implementation of the preceding algorithm in the computer programming language Ruby. It does not use the recursivity, which increases the speed of execution. In the majority of the languages you will need to replace resultat=1 by resultat=matrice_unite_de_la_meme_taille_que_x to obtain the calculation programme of a power of a matrix. In Ruby, resultat adapts automatically to the adapted type, thus this function is used as well with the matrices as with the entireties or realities.

def power (X, N)
    result = 1
    while (N! = 0)
        # if N is odd, one multiplies result by X
        yew ((N % 2) == 1) then
            result = result * X
            n=n-1
        end
        X = x*x
        N = n/2
    end
    return result
end

Another example of implementation, this time in Pascal, and using the recursivity.

function power (X, N: longint);
 

begin yew n=1 then power: =x else yew N MOD 2=0 then (*Si N is pair*) power: =puissance (X, N div 2) *puissance (X, N div 2) else power: =puissance (X, N div 2) *puissance (X, N div 2) *x end;

Lastly, an example in a functional language, OCaml. To work in all generics, one provides to the function a function multiplication (corresponding with * on the entireties) and a neutral element (correspondent with 1). That makes it possible for example to make a fast exponentiation on the polynomials.

let rec neutral power (*) has = function
  0 - > neutral
| N - > let B = power neutral (*) (A * a) (N/2) in
       yew N MOD 2 = 0 then B else A * b

One can as observe as this function makes it possible to make a fast multiplication while making use only of the function of addition: " power 0 (+) 3 7" return 21 well.

See too

  • Continuation of modular Fibonacci
  • Exponentiation

Simple: Exponentiation by squaring

Random links:Split | Hoquet | The Community of communes of the green valleys | Jacky Bonnevay | Fire 1280 | Knud_Johan_Victor_Rasmussen