2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3e61000cf542c50fef9
|
|
|
|
|
title: 'Problem 122: Efficient exponentiation'
|
2020-11-27 18:02:05 +00:00
|
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
|
forumTopicId: 301749
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: problem-122-efficient-exponentiation
|
2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
The most naive way of computing n15 requires fourteen multiplications:
|
2020-11-27 18:02:05 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
n × n × ... × n = n15
|
2020-11-27 18:02:05 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
But using a "binary" method you can compute it in six multiplications:
|
2020-11-27 18:02:05 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
n × n = n2n2 × n2 = n4n4 × n4 = n8n8 × n4 = n12n12 × n2 = n14n14 × n = n15
|
2020-11-27 18:02:05 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
However it is yet possible to compute it in only five multiplications:
|
2020-11-27 18:02:05 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
n × n = n2n2 × n = n3n3 × n3 = n6n6 × n6 = n12n12 × n3 = n15
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
We shall define m(k) to be the minimum number of multiplications to compute nk; for example m(15) = 5.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
For 1 ≤ k ≤ 200, find ∑ m(k).
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --hints--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`euler122()` should return 1582.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(euler122(), 1582);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --seed--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
## --seed-contents--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function euler122() {
|
2020-09-15 16:57:40 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
euler122();
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// solution required
|
|
|
|
|
```
|