How to resolve the algorithm Ethiopian multiplication step by step in the S-BASIC programming language

Published on 12 May 2024 09:40 PM

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

Source code in the s-basic programming language

$constant true  =  0FFFFH
$constant false =  0

function half(n = integer) = integer
end = n / 2

function twice(n = integer) = integer
end = n + n

rem - return true (-1) if n is even, otherwise false 
function even(n = integer) = integer
var one = integer  
one = 1   rem - only variables are compared bitwise
end = ((n and one) = 0)

rem - return i * j, optionally showing steps
function ethiopian(i, j, show = integer) = integer
  var p = integer
  p = 0
  while i >= 1 do
    begin
      if even(i) then
        begin
          if show then print i;" ---";j
        end
      else
        begin
          if show then print i;"    ";j;"+"
          p = p + j
        end
      i = half(i)
      j = twice(j)
    end
  if show then 
    begin
      print "----------"
      print "     =";
    end
end = p

rem - exercise the function
print "Multiplying 17 times 34"
print ethiopian(17,34,true)

end

  

You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Lua programming language
You may also check:How to resolve the algorithm Yellowstone sequence step by step in the Go programming language
You may also check:How to resolve the algorithm Compiler/syntax analyzer step by step in the COBOL programming language
You may also check:How to resolve the algorithm Sexy primes step by step in the Julia programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the Ruby programming language