0

I am trying to find the number of multiplications required when executing an algorithm which uses Exponentiation by Squaring I was reading about on Wikipedia. The section, Computational Complexity, mentions that the algorithm requires at most floor(log n). How could I go about proving this?

I have this pseudocode:

expo(a, n)
    if n == 0 
       return 1
    if n == 1 
       return a
    if n is even
        b = expo(a, n/2)
        return b*b
    return a * expo(a, n-1)

With this, I also have the following relation

Number of multiplications = T(n)

T(n) = 0 if n<2; a*a^(n-1) if n is odd; (a^(n/2))^2 is n is even

I've attempted using bit-strings representing the base, a, and noting binary operations which need to be completed. i.e. 5 = 101_2. All 1's require inverting and then bit-shifting to the right. All 0's simply require bit-shifting to the right. These operations then can represent multiplication, as described by this chart I produced:

exponent n                 0  1  2  3  4  5  6  7  8  
bits in n                  1  1  2  2  3  3  3  3  4  
0-bits in n                1  0  1  0  2  1  1  0  3
1-bits in n                0  1  1  2  1  2  2  1  1 
binary operations for a^n  0  0  1  2  2  3  3  4  3
multiplications for a^n    0  0  1  2  2  3  3  4  3

Edit

As pointed out by Henry in the comments below, the number of multiplications can be found using # of bits in binary representation + # of 1 bits in binary representation - 1. To prevent getting lost in the math, I will assume the amount of 1-bits is given by some function b(n). Then, T(n) = floor(log_2 n) + b(n) - 1

Proving for n = 2:

2_10 = 10_2 -> b(2) = 1

-> T(2) = floor(log_2 2) + b(2) - 1 = 1 + 1 - 1 = 1

This agrees with the observation table above.

Assume true for k.

Prove for k+1:

T(k+1) = floor(log_2 (k+1)) + b(k+1) - 1

After this formula, in terms of k+1, I am not so sure what to do. I would appreciate any insight.

Mant1c0r3
  • 129
  • 1
  • 5
  • Hint: you can find the exact number of multiplications easily if you represent n as a binary number. It is the number of binary digits plus the number of ones in the binary number minus 1. For example n=12 (in binary 1100) has 4 digits and 2 ones which gives a total of 5. – Henry Oct 05 '17 at 05:20
  • By that, this would give T(n) = floor(log_2 n) + # ones -1. Assuming the number of ones in some binary is given by b(n), I have T(n) = floor(log_2 n) + b(n) - 1. – Mant1c0r3 Oct 05 '17 at 05:55
  • Not quite, the number of digits is floor(log_2 n) + 1. – Henry Oct 05 '17 at 05:59
  • Then we have T(n)=floor(log_2 n) + b(n)? – Mant1c0r3 Oct 05 '17 at 06:07
  • 1
    If this is the case, that T(n)=floor(log_2 n) + b(n), I don't understand how it could be correct. Consider T(2). Then floor(log_2 2) + b(2) = 1 + 1 = 2. By observation of some a^n for n=2, a^2 = a * a which requires 1 multiplication, not 2. – Mant1c0r3 Oct 05 '17 at 06:36
  • You are right, i am off by one. Should be "number of binary digits plus the number of ones in the binary number minus 2". – Henry Oct 05 '17 at 06:51

0 Answers0