Ethiopian multiplication: Difference between revisions

No edit summary
Line 5,683:
====================
578
 
== {{header|TypeScript}} ==
{{trans|Modula-2}}
<lang javascript>
// Ethiopian multiplication
 
function intToString(n: number, wdth: number): string {
sn = Math.floor(n).toString();
len = sn.length;
return (wdth < len ? "#".repeat(wdth) : " ".repeat(wdth - len) + sn);
}
 
function double(a: number): number {
return 2 * a;
}
 
function halve(a: number): number {
return Math.floor(a / 2);
}
 
function isEven(a: number): bool {
return a % 2 == 0;
}
 
function showEthiopianMultiplication(x: number, y: number): void {
var tot = 0;
while (x >= 1) {
process.stdout.write(intToString(x, 9) + " ");
if (!isEven(x)) {
tot += y;
process.stdout.write(intToString(y, 9));
}
console.log();
x = halve(x);
y = double(y);
}
console.log("=" + " ".repeat(9) + intToString(tot, 9));
}
 
showEthiopianMultiplication(17, 34);
</lang>
{{out}}
<pre>
17 34
8
4
2
1 544
= 578
</pre>
 
=={{header|UNIX Shell}}==
Anonymous user