2018-09-30 23:01:58 +01:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3e61000cf542c50fef9
|
|
|
|
|
title: 'Problem 122: Efficient exponentiation'
|
2020-11-27 19:02:05 +01:00
|
|
|
|
challengeType: 5
|
2019-08-05 09:17:33 -07:00
|
|
|
|
forumTopicId: 301749
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: problem-122-efficient-exponentiation
|
2018-09-30 23:01:58 +01:00
|
|
|
|
---
|
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-07-16 21:38:37 +02:00
|
|
|
|
The most naive way of computing $n^{15}$ requires fourteen multiplications:
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
2021-07-16 21:38:37 +02:00
|
|
|
|
$$n × n × \ldots × n = n^{15}$$
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
But using a "binary" method you can compute it in six multiplications:
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
2021-07-16 21:38:37 +02:00
|
|
|
|
$$\begin{align}
|
|
|
|
|
& n × n = n^2\\\\
|
|
|
|
|
& n^2 × n^2 = n^4\\\\
|
|
|
|
|
& n^4 × n^4 = n^8\\\\
|
|
|
|
|
& n^8 × n^4 = n^{12}\\\\
|
|
|
|
|
& n^{12} × n^2 = n^{14}\\\\
|
|
|
|
|
& n^{14} × n = n^{15}
|
|
|
|
|
\end{align}$$
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
However it is yet possible to compute it in only five multiplications:
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
2021-07-16 21:38:37 +02:00
|
|
|
|
$$\begin{align}
|
|
|
|
|
& n × n = n^2\\\\
|
|
|
|
|
& n^2 × n = n^3\\\\
|
|
|
|
|
& n^3 × n^3 = n^6\\\\
|
|
|
|
|
& n^6 × n^6 = n^{12}\\\\
|
|
|
|
|
& n^{12} × n^3 = n^{15}
|
|
|
|
|
\end{align}$$
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
2021-07-16 21:38:37 +02:00
|
|
|
|
We shall define $m(k)$ to be the minimum number of multiplications to compute $n^k$; for example $m(15) = 5$.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
2021-07-16 21:38:37 +02:00
|
|
|
|
For $1 ≤ k ≤ 200$, find $\sum{m(k)}$.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
|
# --hints--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
2021-07-16 21:38:37 +02:00
|
|
|
|
`efficientExponentation()` should return `1582`.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
|
```js
|
2021-07-16 21:38:37 +02:00
|
|
|
|
assert.strictEqual(efficientExponentation(), 1582);
|
2018-09-30 23:01:58 +01:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
|
# --seed--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
|
## --seed-contents--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
|
|
```js
|
2021-07-16 21:38:37 +02:00
|
|
|
|
function efficientExponentation() {
|
2020-09-15 09:57:40 -07:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 21:38:37 +02:00
|
|
|
|
efficientExponentation();
|
2018-09-30 23:01:58 +01:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
|
# --solutions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// solution required
|
|
|
|
|
```
|