Fast Exponentiation Algorithms

«  Permissions 0644 for xx are too open
Rabin–Karp algorithm  »

The normal power function would be like:

func expo(_ a: Int, _ b: Int) -> Int {
    var res = 1
    var b = b
    while b != 0 {
        res *= a
        b -= 1
    }
    return res
}

This algorithm take $O(n)$ time.

This could be improved by using Exponentiation by squaring, which will take $O(\log n)$ time.

exponentiating by squaring is a general method for fast computation of large positive integer powers of a number, or more generally of an element of a semigroup, like a polynomial or a square matrix.

Some variants are commonly referred to as square-and-multiply algorithms or binary exponentiation. These can be of quite general use, for example in modular arithmetic or powering of matrices. For semigroups for which additive notation is commonly used, like elliptic curves used in cryptography, this method is also referred to as double-and-add.

Here is its basic idea:

\[x^{n}={ \begin{cases} 1, & \text{if } n = 0 \\ {\frac{1}{x}}^{-n}, & \text{if } n < 0 \\ x\,(x^{2})^{\frac {n-1}{2}}, & \text{if } n \text{ is odd.} \\ (x^{2})^{\frac {n}{2}}, & \text{if } n \text{ is even.} \end{cases} }\]

Swift Implementation

func expo(_ a: Int, _ b: Int) -> Int {
    var res = 1
    var a = a
    var b = b
    while b != 0 {
        if b & 1 == 1 {
            res *= a
        }
        b >>= 1
        a *= a
    }
    return res
}

Refernces

Published on 28 Mar 2021 Find me on Facebook, Twitter!

«  Permissions 0644 for xx are too open
Rabin–Karp algorithm  »

Comments

    Join the discussion for this article at here . Our comments is using Github Issues. All of posted comments will display at this page instantly.