user
Ethiopian multiplication is a method of multiplying integers using only addition, doubling, and halving.

**Method:**

1. Take two numbers to be multiplied and write them down at the top of two columns.
2. 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 1.
3. 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 1.
4. Examine the table produced and discard any row where the value in the left column is even.
5. Sum the values in the right-hand column that remain to produce the result of multiplying the original two numbers together

**For example:**   17 × 34

```
       17    34

```

Halving the first column:

```
       17    34
        8
        4
        2
        1

```

Doubling the second column:

```
       17    34
        8    68
        4   136 
        2   272
        1   544

```

Strike-out rows whose first cell is even:

```
       17    34
        8    68 
        4   136 
        2   272 
        1   544

```

Sum the remaining numbers in the right-hand column:

```
       17    34
        8    -- 
        4   --- 
        2   --- 
        1   544
           ====
            578

```

So 17 multiplied by 34, by the Ethiopian method is 578.

Task

The task is to **define three named functions**/methods/procedures/subroutines:

1. one to **halve an integer**,
2. one to **double an integer**, and
3. one to **state if an integer is even**.

Use these functions to **create a function that does Ethiopian multiplication**.

Related tasks

* Egyptian division

References

* Ethiopian multiplication explained (BBC Video clip)
* A Night Of Numbers - Go Forth And Multiply (Video)
* Russian Peasant Multiplication
* Programming Praxis: Russian Peasant Multiplication
assistant
(formerly Perl 6)

```
sub halve  (Int $n is rw)    { $n div= 2 }
sub double (Int $n is rw)    { $n *= 2 }
sub even   (Int $n --> Bool) { $n %% 2 }

sub ethiopic-mult (Int $a is copy, Int $b is copy --> Int) {
    my Int $r = 0;
    while $a {
	even $a or $r += $b;
	halve $a;
	double $b;
    }
    return $r;
}

say ethiopic-mult(17,34);
```

Output:

```
578

```

More succinctly using implicit typing, primed lambdas, and an infinite loop:

```
sub ethiopic-mult {
    my &halve  = * div= 2;
    my &double = * *= 2;
    my &even   = * %% 2;

    my ($a,$b) = @_;
    my $r;
    loop {
        even  $a or $r += $b;
        halve $a or return $r;
        double $b;
    }
}

say ethiopic-mult(17,34);
```

More succinctly still, using a pure functional approach (reductions, mappings, lazy infinite sequences):

```
sub halve  { $^n div 2 }
sub double { $^n * 2   }
sub even   { $^n %% 2  }

sub ethiopic-mult ($a, $b) {
    [+] ($b, &double ... *)
        Z*
        ($a, &halve ... 0).map: { not even $^n }
}

say ethiopic-mult(17,34);
```

(same output)