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

Published on 12 May 2024 09:40 PM

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

Source code in the perl programming language

use strict;

sub halve { int((shift) / 2); }
sub double { (shift) * 2; }
sub iseven { ((shift) & 1) == 0; }

sub ethiopicmult
{
    my ($plier, $plicand, $tutor) = @_;
    print "ethiopic multiplication of $plier and $plicand\n" if $tutor;
    my $r = 0;
    while ($plier >= 1)
    {
	$r += $plicand unless iseven($plier);
	if ($tutor) {
	    print "$plier, $plicand ", (iseven($plier) ? " struck" : " kept"), "\n";
	}
	$plier = halve($plier);
	$plicand = double($plicand);
    }
    return $r;
}

print ethiopicmult(17,34, 1), "\n";


  

You may also check:How to resolve the algorithm Range expansion step by step in the Lua programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Loops/For step by step in the x86 Assembly programming language
You may also check:How to resolve the algorithm Elliptic Curve Digital Signature Algorithm step by step in the Raku programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the Fermat programming language