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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ethiopian multiplication step by step in the CLU 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 CLU programming language

Source code in the clu programming language

halve = proc (n: int) returns (int)
    return(n/2)
end halve

double = proc (n: int) returns (int)
    return(n*2)
end double

even = proc (n: int) returns (bool)
    return(n//2 = 0)
end even

e_mul = proc (a, b: int) returns (int)
    total: int := 0
    
    while (a > 0) do
        if ~even(a) then total := total + b end
        a := halve(a)
        b := double(b)
    end
    
    return(total)
end e_mul

start_up = proc ()  
    po: stream := stream$primary_output()
    stream$putl(po, int$unparse(e_mul(17, 34)))
end start_up

  

You may also check:How to resolve the algorithm FizzBuzz step by step in the Maxima programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the VBA programming language
You may also check:How to resolve the algorithm Factorions step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the C# programming language
You may also check:How to resolve the algorithm Program termination step by step in the Neko programming language