How to resolve the algorithm Ethiopian multiplication step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Ethiopian multiplication step by step in the Prolog 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 Prolog programming language
Source code in the prolog programming language
halve(X,Y) :- Y is X // 2.
double(X,Y) :- Y is 2*X.
is_even(X) :- 0 is X mod 2.
% columns(First,Second,Left,Right) is true if integers First and Second
% expand into the columns Left and Right, respectively
columns(1,Second,[1],[Second]).
columns(First,Second,[First|Left],[Second|Right]) :-
halve(First,Halved),
double(Second,Doubled),
columns(Halved,Doubled,Left,Right).
% contribution(Left,Right,Amount) is true if integers Left and Right,
% from their respective columns contribute Amount to the final sum.
contribution(Left,_Right,0) :-
is_even(Left).
contribution(Left,Right,Right) :-
\+ is_even(Left).
ethiopian(First,Second,Product) :-
columns(First,Second,Left,Right),
maplist(contribution,Left,Right,Contributions),
sumlist(Contributions,Product).
:- use_module(library(func)).
% halve/2, double/2, is_even/2 definitions go here
ethiopian(First,Second,Product) :-
ethiopian(First,Second,0,Product).
ethiopian(1,Second,Sum0,Sum) :-
Sum is Sum0 + Second.
ethiopian(First,Second,Sum0,Sum) :-
Sum1 is Sum0 + Second*(First mod 2),
ethiopian(halve $ First, double $ Second, Sum1, Sum).
:- module(ethiopia, [test/0, mul/3]).
:- use_module(library(chr)).
:- chr_constraint mul/3, halve/2, double/2, even/1, add_odd/4.
mul(1, Y, S) <=> S = Y.
mul(X, Y, S) <=> X \= 1 | halve(X, X1),
double(Y, Y1),
mul(X1, Y1, S1),
add_odd(X, Y, S1, S).
halve(X, Y) <=> Y is X // 2.
double(X, Y) <=> Y is X * 2.
even(X) <=> 0 is X mod 2 | true.
even(X) <=> 1 is X mod 2 | false.
add_odd(X, _, A, S) <=> even(X) | S is A.
add_odd(X, Y, A, S) <=> \+ even(X) | S is A + Y.
test :-
mul(17, 34, Z), !,
writeln(Z).
:- module(ethiopia, [test/0, mul/3]).
:- use_module(library(chr)).
:- chr_constraint mul/3, even/1, add_if_odd/4.
mul(1, Y, S) <=> S = Y.
mul(X, Y, S) <=> X \= 1 | X1 is X // 2,
Y1 is Y * 2,
mul(X1, Y1, S1),
add_if_odd(X, Y, S1, S).
even(X) <=> 0 is X mod 2 | true.
even(X) <=> 1 is X mod 2 | false.
add_if_odd(X, _, A, S) <=> even(X) | S is A.
add_if_odd(X, Y, A, S) <=> \+ even(X) | S is A + Y.
test :-
mul(17, 34, Z),
writeln(Z).
You may also check:How to resolve the algorithm Polynomial long division step by step in the APL programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Set lang programming language
You may also check:How to resolve the algorithm Kronecker product step by step in the Factor programming language
You may also check:How to resolve the algorithm Date format step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the C/C++ programming language