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

Published on 12 May 2024 09:40 PM

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

Source code in the maxima programming language

/* Function to halve */
halve(n):=floor(n/2)$

/* Function to double */
double(n):=2*n$

/* Predicate function to check wether an integer is even */
my_evenp(n):=if mod(n,2)=0 then true$

/* Function that implements ethiopian function using the three previously defined functions */
ethiopian(n1,n2):=block(cn1:n1,cn2:n2,list_w:[],
    while cn1>0 do (list_w:endcons(cn1,list_w),cn1:halve(cn1)),
    n2_list:append([cn2],makelist(cn2:double(cn2),length(list_w)-1)),
    sublist_indices(list_w,lambda([x],not my_evenp(x))),
    makelist(n2_list[i],i,%%),
    apply("+",%%))$


  

You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the Ruby programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the Fortran programming language
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Pharo programming language