How to resolve the algorithm Ethiopian multiplication step by step in the JavaScript programming language
How to resolve the algorithm Ethiopian multiplication step by step in the JavaScript 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 JavaScript programming language
Objective:
The provided JavaScript code defines two functions, eth.mult
and ethMult
, that perform fast integer multiplication using the "Ethiopian multiplication" method. This method was traditionally used in ancient Ethiopia and is known for its efficiency and simplicity.
Implementation Details:
A. eth.mult
Function:
- The
eth.mult
function takes two integer arguments,a
andb
, and returns their product. - It starts by initializing
sum
to 0 and convertinga
andb
to arrays with a single element each. - It then iteratively halves
a
and doublesb
untila[0]
becomes 1 (indicating that it's reached the limit). - For each iteration, it checks if
a[i]
is odd. If it is, it addsb[i]
to thesum
. - Finally, it adds
b[0]
tosum
and returns the result.
B. ethMult
Function:
- The
ethMult
function is a more concise implementation of the Ethiopian multiplication method. - It handles both integers and strings (for cases like concatenating strings).
- It initializes
o
to an empty string if the input is a string, or 0 if it's an integer. - It then iteratively performs the following steps:
- If
n
is odd (determined by a bitwise AND operation with 1), it addsm
too
. - It right-shifts
n
by 1, which is equivalent to integer division by 2. - It adds
m
to itself, which is equivalent to integer multiplication by 2.
- If
- It repeats this process until
n
is less than or equal to 1. - Finally, it returns the concatenation of
o
withm
(for strings) oro
+m
(for integers).
Example Usage:
eth.mult(17, 34) // returns 578
- ```javascript
ethMult(17, 34) // returns 578
ethMult('Ethiopian', 34) // returns 'EthiopianEthiopianEthiopian...' (34 times)
<div id="sourcecode"/>
## Source code in the javascript programming language
```javascript
var eth = {
halve : function ( n ){ return Math.floor(n/2); },
double: function ( n ){ return 2*n; },
isEven: function ( n ){ return n%2 === 0); },
mult: function ( a , b ){
var sum = 0, a = [a], b = [b];
while ( a[0] !== 1 ){
a.unshift( eth.halve( a[0] ) );
b.unshift( eth.double( b[0] ) );
}
for( var i = a.length - 1; i > 0 ; i -= 1 ){
if( !eth.isEven( a[i] ) ){
sum += b[i];
}
}
return sum + b[0];
}
}
// eth.mult(17,34) returns 578
function ethMult(m, n) {
var o = !isNaN(m) ? 0 : ''; // same technique works with strings
if (n < 1) return o;
while (n > 1) {
if (n & 1) o += m; // 3. integer odd/even? (bit-wise and 1)
n >>= 1; // 1. integer halved (by right-shift)
m += m; // 2. integer doubled (addition to self)
}
return o + m;
}
ethMult(17, 34)
ethMult('Ethiopian', 34)
You may also check:How to resolve the algorithm Idoneal numbers step by step in the Action! programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the FileMaker programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the AppleScript programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the Slope programming language
You may also check:How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the Ruby programming language