How to resolve the algorithm Ethiopian multiplication step by step in the Icon and Unicon programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Ethiopian multiplication step by step in the Icon and Unicon 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 Icon and Unicon programming language
Source code in the icon programming language
procedure main(arglist)
while ethiopian(integer(get(arglist)),integer(get(arglist))) # multiply successive pairs of command line arguments
end
procedure ethiopian(i,j) # recursive Ethiopian multiplication
return ( if not even(i) then j # this exploits that icon control expressions return values
else 0 ) +
( if i ~= 0 then ethiopian(halve(i),double(j))
else 0 )
end
procedure double(i)
return i * 2
end
procedure halve(i)
return i / 2
end
procedure even(i)
return ( i % 2 = 0, i )
end
procedure ethiopian(i,j) # iterative tutor
local p,w
w := *j+3
write("Ethiopian Multiplication of ",i," * ",j)
p := 0
until i = 0 do {
writes(right(i,w),right(j,w))
if not even(i) then {
p +:= j
write(" add")
}
else write(" discard")
i := halve(i)
j := double(j)
}
write(right("=",w),right(p,w))
return p
end
You may also check:How to resolve the algorithm Square-free integers step by step in the Arturo programming language
You may also check:How to resolve the algorithm Quine step by step in the Oz programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Scala programming language
You may also check:How to resolve the algorithm Cramer's rule step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Scheme programming language