How to resolve the algorithm Ethiopian multiplication step by step in the Julia programming language
How to resolve the algorithm Ethiopian multiplication step by step in the Julia 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 Julia programming language
The provided Julia code defines three functions: halve
, double
, and even
which are used for bitwise operations on integers, and two functions ethmult
and ethmult2
which implement the Euclidean multiplication algorithm for multiplying two integers.
Here's a detailed explanation of the code:
-
halve(x::Integer)
: This function performs a bitwise shift of the input integerx
by one bit to the right, effectively halving its value. -
double(x::Integer)
: This function multiplies the input integerx
by two, which is equivalent to a bitwise shift ofx
by one bit to the left. -
even(x::Integer)
: This function checks if the input integerx
is even by performing a bitwise AND operation ofx
with 1. If the result is 0,x
is even; otherwise, it is odd. -
ethmult(a::Integer, b::Integer)
: This is the first implementation of the Euclidean multiplication algorithm. It iteratively halvesa
and doublesb
untila
becomes 0. Whilea
is greater than 0, the algorithm addsb
to the resultr
ifa
is odd (by checking if!even(a)
is true). -
ethmult2(a::Integer, b::Integer)
: This is a more efficient implementation of the Euclidean multiplication algorithm. It uses a vectorA
to store the values ofa
as it is halved and a vectorB
to store the values ofb
as it is doubled. The algorithm continues untilA[end]
(the last element ofA
) becomes 1. Then, it sums the elements ofB
that correspond to the odd values ofA
(by using the!even
function applied toA
) to get the final result.
The code also includes two calls to @show
to display the results of ethmult
and ethmult2
for the inputs a=17
and b=34
.
Source code in the julia programming language
halve(x::Integer) = x >> one(x)
double(x::Integer) = Int8(2) * x
even(x::Integer) = x & 1 != 1
function ethmult(a::Integer, b::Integer)
r = 0
while a > 0
r += b * !even(a)
a = halve(a)
b = double(b)
end
return r
end
@show ethmult(17, 34)
function ethmult2(a::Integer, b::Integer)
A = [a]
B = [b]
while A[end] > 1
push!(A, halve(A[end]))
push!(B, double(B[end]))
end
return sum(B[map(!even, A)])
end
@show ethmult2(17, 34)
You may also check:How to resolve the algorithm Anonymous recursion step by step in the Wren programming language
You may also check:How to resolve the algorithm Twin primes step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Set of real numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Befunge programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the Common Lisp programming language