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

Published on 22 June 2024 08:30 PM

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 integer x by one bit to the right, effectively halving its value.

  • double(x::Integer): This function multiplies the input integer x by two, which is equivalent to a bitwise shift of x by one bit to the left.

  • even(x::Integer): This function checks if the input integer x is even by performing a bitwise AND operation of x 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 halves a and doubles b until a becomes 0. While a is greater than 0, the algorithm adds b to the result r if a 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 vector A to store the values of a as it is halved and a vector B to store the values of b as it is doubled. The algorithm continues until A[end] (the last element of A) becomes 1. Then, it sums the elements of B that correspond to the odd values of A (by using the !even function applied to A) 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