How do you efficiently compute xn for a positive integer n? Take x15 as an example. You could take x and repeatedly multiply by x 14 times. A better way to do it, however, is this:
- t0=x
- t1=t0⋅t0=x2
- t2=t0⋅t1=x3
- t3=t1⋅t2=x5
- t4=t3⋅t3=x10
- t5=t3⋅t4=x15
A shorter way to write this is x1,x2,x3,x5,x10,x15, where each quantity is obtained by multiplying two of the previous quantities together. We can write it even shorter as 1,2,3,5,10,15, where only the exponents are written. Here each number is obtained by adding together two of the previous numbers. This is called an addition chain and is at the heart of studying the optimal way of evaluating powers. There is no simple expression that computes the minimal number of multiplications a(n) needed to evaluate xn. A list, however, is available from The On-Line Encyclopedia of Integer Sequences, where the first 40 entries are
0, 1, 2, 2, 3, 3, 4, 3, 4, 4, 5, 4, 5, 5, 5, 4, 5, 5, 6, 5, 6, 6, 6, 5, 6, 6, 6, 6, 7, 6, 7, 5, 6, 6, 7, 6, 7, 7, 7, 6, …
We see that a(15)=5, which shows that the addition chain 1,2,3,5,10,15 is indeed the shortest possible, and it follows that the procedure shown above to compute x15 required the minimal number of multiplications.
Since the numbers in an addition chain grows the fastest by doubling the previous item, it is fairly easy to see that
(1)
a(n)≥⌈log2(n)⌉.
Read full article from Evaluation of Powers - janmr blog
No comments:
Post a Comment