2018-09-30 22:01:58 +00:00
---
id: 599d1566a02b571412643b84
2020-11-27 18:02:05 +00:00
title: Ethiopian multiplication
2018-09-30 22:01:58 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302257
2021-01-13 02:31:00 +00:00
dashedName: ethiopian-multiplication
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2019-03-02 08:42:56 +00:00
Ethiopian multiplication is a method of multiplying integers using only addition, doubling, and halving.
2020-11-27 18:02:05 +00:00
**Method:**
2019-03-02 08:42:56 +00:00
< ol >
2019-06-14 11:04:16 +00:00
< li > Take two numbers to be multiplied and write them down at the top of two columns< / li >
< li > In the left-hand column repeatedly halve the last number, discarding any remainders, and write the result below the last in the same column, until you write a value of < code > 1< / code > < / li >
< li > In the right-hand column repeatedly double the last number and write the result below. stop when you add a result in the same row as where the left hand column shows < code > 1< / code > < / li >
< li > Examine the table produced and discard any row where the value in the left column is even< / li >
2019-03-02 08:42:56 +00:00
< li > Sum the values in the right-hand column that remain to produce the result of multiplying the original two numbers together< / li >
< / ol >
2020-11-27 18:02:05 +00:00
**For example:** `17 × 34`
< pre > 17 34
2019-03-02 08:42:56 +00:00
< / pre >
2020-11-27 18:02:05 +00:00
2019-03-02 08:42:56 +00:00
Halving the first column:
2020-11-27 18:02:05 +00:00
< pre > 17 34
2019-03-02 08:42:56 +00:00
8
4
2
1
< / pre >
2020-11-27 18:02:05 +00:00
2019-03-02 08:42:56 +00:00
Doubling the second column:
2020-11-27 18:02:05 +00:00
< pre > 17 34
2019-03-02 08:42:56 +00:00
8 68
4 136
2 272
1 544
< / pre >
2020-11-27 18:02:05 +00:00
2019-03-02 08:42:56 +00:00
Strike-out rows whose first cell is even:
2020-11-27 18:02:05 +00:00
< pre > 17 34
2019-03-02 08:42:56 +00:00
8 < strike > 68< / strike >
4 < strike > 136< / strike >
2 < strike > 272< / strike >
1 544
< / pre >
2020-11-27 18:02:05 +00:00
2019-03-02 08:42:56 +00:00
Sum the remaining numbers in the right-hand column:
2020-11-27 18:02:05 +00:00
<!-- markdownlint - disable MD003 -->
< pre > 17 34
2019-03-02 08:42:56 +00:00
8 --
4 ---
2 ---
1 544
====
578
< / pre >
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
<!-- markdownlint - enable MD003 -->
So `17` multiplied by `34` , by the Ethiopian method is `578` .
# --instructions--
2019-03-02 08:42:56 +00:00
The task is to define three named functions/methods/procedures/subroutines:
2020-11-27 18:02:05 +00:00
2019-03-02 08:42:56 +00:00
< ol >
< li > one to halve an integer,< / li >
< li > one to double an integer, and< / li >
2019-06-14 11:04:16 +00:00
< li > one to state if an integer is even< / li >
2019-03-02 08:42:56 +00:00
< / ol >
2020-11-27 18:02:05 +00:00
2019-03-02 08:42:56 +00:00
Use these functions to create a function that does Ethiopian multiplication.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
<!-- markdownlint - disable MD046 -->
2021-01-13 02:31:00 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
`eth_mult` should be a function.
```js
assert(typeof eth_mult === 'function');
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
`eth_mult(17,34)` should return `578` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert.equal(eth_mult(17, 34), 578);
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`eth_mult(23,46)` should return `1058` .
2018-09-30 22:01:58 +00:00
```js
2020-11-27 18:02:05 +00:00
assert.equal(eth_mult(23, 46), 1058);
```
2020-09-15 16:57:40 +00:00
2020-11-27 18:02:05 +00:00
`eth_mult(12,27)` should return `324` .
```js
assert.equal(eth_mult(12, 27), 324);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
`eth_mult(56,98)` should return `5488` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert.equal(eth_mult(56, 98), 5488);
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`eth_mult(63,74)` should return `4662` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert.equal(eth_mult(63, 74), 4662);
```
# --seed--
## --seed-contents--
```js
function eth_mult(a, b) {
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
}
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
function eth_mult(a, b) {
let sum = 0; a = [a]; b = [b];
let half = a => a / 2,
double = a => a * 2,
is_even = a => a % 2 == 0;
while (a[0] !== 1) {
a.unshift(Math.floor(half(a[0])));
b.unshift(double(b[0]));
}
for (let i = a.length - 1; i > 0; i -= 1) {
if (!is_even(a[i])) {
sum += b[i];
}
}
return sum + b[0];
}
```