How to resolve the algorithm Ethiopian multiplication step by step in the Red programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Ethiopian multiplication step by step in the Red 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 Red programming language
Source code in the red programming language
Red["Ethiopian multiplication"]
halve: function [n][n >> 1]
double: function [n][n << 1]
;== even? already exists
ethiopian-multiply: function [
"Returns the product of two integers using Ethiopian multiplication"
a [integer!] "The multiplicand"
b [integer!] "The multiplier"
][
result: 0
while [a <> 0][
if odd? a [result: result + b]
a: halve a
b: double b
]
result
]
print ethiopian-multiply 17 34
You may also check:How to resolve the algorithm Anonymous recursion step by step in the Julia programming language
You may also check:How to resolve the algorithm Left factorials step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Futhark programming language
You may also check:How to resolve the algorithm Determinant and permanent step by step in the Ol programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Avail programming language