How to resolve the algorithm Ethiopian multiplication step by step in the Pike programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ethiopian multiplication step by step in the Pike programming language

Table of Contents

Problem Statement

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

Method:

For example:   17 × 34 Halving the first column: Doubling the second column: Strike-out rows whose first cell is even: Sum the remaining numbers in the right-hand column: So 17 multiplied by 34, by the Ethiopian method is 578.

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

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ethiopian multiplication step by step in the Pike programming language

Source code in the pike programming language

int ethopian_multiply(int l, int r)
{
    int halve(int n) { return n/2; };
    int double(int n) { return n*2; };
    int(0..1) evenp(int n) { return !(n%2); };

    int product = 0;
    do
    {
        write("%5d %5d\n", l, r);
        if (!evenp(l))
            product += r;
        l = halve(l);
        r = double(r);          
    }
    while(l);
    return product;
}


  

You may also check:How to resolve the algorithm Pascal's triangle step by step in the q programming language
You may also check:How to resolve the algorithm Factorial step by step in the Wren programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Lua programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the Haskell programming language
You may also check:How to resolve the algorithm Magnanimous numbers step by step in the ALGOL W programming language