MCS-211 · Block 1 Unit 3

Complexity Analysis of Simple Algorithms

Section 5 of 8

Block 1 · Unit 3Complexity Analysis of Simple Algorithms6 min · Section 5 of 8

3.5Exponent Evaluation: Binary Exponentiation

Covers IGNOU MCS-211 §3.4.4

Computing a power ana^n by multiplying aa by itself takes n−1n - 1 multiplications. For the huge exponents used in cryptography (exponents with hundreds of digits) that is impossible. Binary exponentiation uses the binary digits of nn to need only about 2log⁡2n2\log_2 n multiplications.

The idea

Write nn in binary. Each bit tells you whether a power a2ia^{2^i} is part of the product, and each such power is the square of the previous one.

A positive integer nn has s=⌊log⁡2n⌋+1s = \lfloor \log_2 n \rfloor + 1 bits, so 2s−1≤n<2s2^{s-1} \le n < 2^s.

Left-to-right binary exponentiation

Scan the bits from the most significant (always 1) to the least significant. Let b[i] be bit ii of nn (b[0]b[0] is the least significant).

Left-to-right pseudocode
LeftToRightPower(a, b[0..s−1])        // b[s−1] = 1 is the leading bit
result ← a
for i ← s − 2 downto 0 do
    result ← result * result          // square
    if b[i] = 1 then
        result ← result * a           // multiply by a
return result

Each step doubles the exponent and adds the next bit, just like reading a binary number from left to right.

Right-to-left binary exponentiation

Scan the bits from the least significant. Keep x=a2ix = a^{2^i} and multiply it into the result when bit ii is 1.

Right-to-left pseudocode
RightToLeftPower(a, b[0..s−1])
x ← a
if b[0] = 1 then result ← a else result ← 1
for i ← 1 to s − 1 do
    x ← x * x                         // x = a^(2^i)
    if b[i] = 1 then
        result ← result * x
return result
C · binary_power.c
#include <stdio.h>

/* Left-to-right: scan the bits of n from the most significant one. */
unsigned long long power_lr(unsigned long long a, unsigned int n, int *mults)
{
    int s = 0;
    for (unsigned int t = n; t > 0; t >>= 1)  /* s = number of bits of n */
        s++;
    unsigned long long result = a;            /* the leading bit is 1   */
    *mults = 0;
    for (int i = s - 2; i >= 0; i--) {
        result = result * result;  (*mults)++;
        if ((n >> i) & 1u) {
            result = result * a;   (*mults)++;
        }
    }
    return result;
}

/* Right-to-left: scan the bits from the least significant one. */
unsigned long long power_rl(unsigned long long a, unsigned int n, int *mults)
{
    unsigned long long x = a;
    unsigned long long result = (n & 1u) ? a : 1;
    *mults = 0;
    for (n >>= 1; n > 0; n >>= 1) {
        x = x * x;                 (*mults)++;
        if (n & 1u) {
            result = result * x;   (*mults)++;
        }
    }
    return result;
}

int main(void)
{
    int m1, m2;
    unsigned long long r1 = power_lr(4, 11, &m1), r2 = power_rl(4, 11, &m2);
    printf("4^11: left-to-right = %llu (%d mults), right-to-left = %llu (%d mults)\n", r1, m1, r2, m2);
    r1 = power_lr(3, 17, &m1);
    r2 = power_rl(3, 17, &m2);
    printf("3^17: left-to-right = %llu (%d mults), right-to-left = %llu (%d mults)\n", r1, m1, r2, m2);
    return 0;
}
Output
4^11: left-to-right = 4194304 (5 mults), right-to-left = 4194304 (5 mults)
3^17: left-to-right = 129140163 (5 mults), right-to-left = 129140163 (5 mults)

Analysis

In both methods, each of the s−1s - 1 loop iterations does one squaring and at most one extra multiplication. If f(n)f(n) is the number of multiplications, s−1≤f(n)≤2(s−1),s−1=⌊log⁡2n⌋.s - 1 \le f(n) \le 2(s - 1), \qquad s - 1 = \lfloor \log_2 n \rfloor . So f(n)=Θ(log⁡n)f(n) = \Theta(\log n): between ⌊log⁡2n⌋\lfloor \log_2 n \rfloor and 2⌊log⁡2n⌋2\lfloor \log_2 n \rfloor multiplications, compared with n−1n - 1 for brute force. More precisely, f(n)=(s−1)+(number of 1-bits)−1f(n) = (s - 1) + (\text{number of 1-bits}) - 1.

Tracing a283a^{283} (Check Your Progress 3)

283=256+16+8+2+1=1000110112283 = 256 + 16 + 8 + 2 + 1 = 100011011_2, so s=9s = 9. The program tracks only the exponent of aa:

Python · exp_trace.py
# Trace binary exponentiation by tracking only the exponent of a.
def left_to_right(n):
    bits = bin(n)[2:]                 # most significant bit first
    e, mults = 1, 0                   # result = a^1
    print(f"n = {n} = {bits} (binary), s = {len(bits)}")
    for pos, bit in enumerate(bits[1:], start=1):
        i = len(bits) - 1 - pos
        e, mults = 2 * e, mults + 1
        if bit == "1":
            e, mults = e + 1, mults + 1
        print(f"  i = {i}, bit = {bit}: result = a^{e}")
    print(f"  left-to-right multiplications: {mults}")

def right_to_left(n):
    bits = bin(n)[2:][::-1]           # least significant bit first
    x, mults = 1, 0                   # x = a^1
    r = 1 if bits[0] == "1" else 0    # result = a^r
    for i, bit in enumerate(bits[1:], start=1):
        x, mults = 2 * x, mults + 1
        if bit == "1":
            r, mults = r + x, mults + (1 if r > 0 else 0)
        print(f"  i = {i}, bit = {bit}: x = a^{x}, result = a^{r}")
    print(f"  right-to-left multiplications: {mults}")

left_to_right(283)
right_to_left(283)
Output
n = 283 = 100011011 (binary), s = 9
  i = 7, bit = 0: result = a^2
  i = 6, bit = 0: result = a^4
  i = 5, bit = 0: result = a^8
  i = 4, bit = 1: result = a^17
  i = 3, bit = 1: result = a^35
  i = 2, bit = 0: result = a^70
  i = 1, bit = 1: result = a^141
  i = 0, bit = 1: result = a^283
  left-to-right multiplications: 12
  i = 1, bit = 1: x = a^2, result = a^3
  i = 2, bit = 0: x = a^4, result = a^3
  i = 3, bit = 1: x = a^8, result = a^11
  i = 4, bit = 1: x = a^16, result = a^27
  i = 5, bit = 0: x = a^32, result = a^27
  i = 6, bit = 0: x = a^64, result = a^27
  i = 7, bit = 0: x = a^128, result = a^27
  i = 8, bit = 1: x = a^256, result = a^283
  right-to-left multiplications: 12

Both methods use 88 squarings plus 44 extra multiplications (283283 has five 1-bits, and the first one costs nothing) = 12 multiplications, instead of 282. The study material’s two tables for a283a^{283} agree with these traces.

Key takeaways

  • nn has s=⌊log⁡2n⌋+1s = \lfloor \log_2 n \rfloor + 1 bits; ana^n is the product of the powers a2ia^{2^i} whose bit is 1.
  • Left to right: square, then multiply by aa if the bit is 1.
  • Right to left: square xx, then multiply the result by xx if the bit is 1.
  • Multiplications: between s−1s - 1 and 2(s−1)2(s - 1), i.e. Θ(log⁡n)\Theta(\log n); a283a^{283} needs 12.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many bits does n=100n = 100 have?

Show answer

Answer: B. 7

2⁶ = 64 ≤ 100 < 128 = 2⁷, so s = ⌊log₂100⌋ + 1 = 7. The binary form is 1100100.

Question 2 · multiple choice

How many multiplications does left-to-right binary exponentiation use for a16a^{16}?

Show answer

Answer: D. 4

16 = 10000₂: four squarings (a², a⁴, a⁸, a¹⁶) and no extra multiplications. 15 is brute force.

Question 3 · multiple choice

In left-to-right exponentiation for n=13=11012n = 13 = 1101_2, what is the result after processing the second bit (from the left)?

Show answer

Answer: B. a3a^3

Start with a¹ (first bit). Second bit is 1: square → a², multiply by a → a³. Then bit 0 gives a⁶ and bit 1 gives a¹³.

Question 4 · multiple choice

What is the largest number of multiplications binary exponentiation can use when nn has s=6s = 6 bits?

Show answer

Answer: C. 10

At most 2(s − 1) = 10, reached when all bits are 1 (n = 63). 63 − 1 = 62 would be brute force.

Question 5 · multiple choice

Which statement about the right-to-left method is correct?

Show answer

Answer: A. It keeps x=a2ix = a^{2^i} by squaring and multiplies xx into the result when bit ii is 1.

That describes the algorithm. Starting with the most significant bit is the left-to-right method.

Question 6 · short answer

Compute a22a^{22} with left-to-right binary exponentiation, showing each step and the number of multiplications.

Show worked answer

22 = 10110₂, s = 5. Start result = a (leading bit). Bit 0: square → a²; no multiply. Bit 1: square → a⁴, multiply by a → a⁵. Bit 1: square → a¹⁰, multiply → a¹¹. Bit 0: square → a²². Multiplications: 4 squarings + 2 extra = 6 (instead of 21).

↑ ↓ move↵ openTab switch type