How to resolve the algorithm Ethiopian multiplication step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ethiopian multiplication step by step in the V (Vlang) 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 V (Vlang) programming language

Source code in the v programming language

fn halve(i int) int { return i/2 }
 
fn double(i int) int { return i*2 }
 
fn is_even(i int) bool { return i%2 == 0 }
 
fn eth_multi(ii int, jj int) int {
    mut r := 0
    mut i, mut j := ii, jj
    for ; i > 0; i, j = halve(i), double(j) {
        if !is_even(i) {
            r += j
        }
    }
    return r
}
 
fn main() {
    println("17 ethiopian 34 = ${eth_multi(17, 34)}")
}

  

You may also check:How to resolve the algorithm Spiral matrix step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Null object step by step in the Objeck programming language
You may also check:How to resolve the algorithm Sierpinski arrowhead curve step by step in the Prolog programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the Lua programming language