Problem 122
Efficient exponentiation
The most naive way of computing n15 requires fourteen multiplications:
n × n × … × n = n15
But using a “binary” method you can compute it in six multiplications:
n × n = n2
n2 × n2 = n4
n4 × n4 = n8
n8 × n4 = n12
n12 × n2 = n14
n14 × n = n15
However it is yet possible to compute it in only five multiplications:
n × n = n2
n2 × n = n3
n3 × n3 = n6
n6 × n6 = n12
n12 × n3 = n15
We shall define m(k) to be the minimum number of multiplications to compute nk; for example m(15) = 5.
For 1 ≤ k ≤ 200, find ∑m(k).
高效指数计算
计算n15最朴素的方式需要14次乘法:
n × n × … × n = n15
但使用一个“二进制”的算法,你可以只用6次乘法:
n × n = n2
n2 × n2 = n4
n4 × n4 = n8
n8 × n4 = n12
n12 × n2 = n14
n14 × n = n15
然而,实际上仅用5次乘法也是可以的:
n × n = n2
n2 × n = n3
n3 × n3 = n6
n6 × n6 = n12
n12 × n3 = n15
记m(k)是计算nk所需要的最少次数,例如m(15) = 5。
对于1 ≤ k ≤ 200,求∑m(k)。