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

Published on 12 May 2024 09:40 PM

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

Source code in the seed7 programming language

const proc: double (inout integer: a) is func
  begin
    a *:= 2;
  end func;

const proc: halve (inout integer: a) is func
  begin
    a := a div 2;
  end func;

const func boolean: even (in integer: a) is
  return not odd(a);

const func integer: peasantMult (in var integer: a, in var integer: b) is func
  result
    var integer: result is 0;
  begin
    while a <> 0 do
      if not even(a) then
        result +:= b;
      end if;
      halve(a);
      double(b);
    end while;
  end func;

  

You may also check:How to resolve the algorithm Fairshare between two and more step by step in the Phix programming language
You may also check:How to resolve the algorithm Combinations step by step in the J programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the Lua programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Tcl programming language