Ethiopian multiplication: Difference between revisions

Content added Content deleted
(Updated D entry)
Line 926: Line 926:


=={{header|D}}==
=={{header|D}}==
<lang d>int ethiopian(int n1, int n2) pure nothrow
<lang d>import std.stdio;

int ethiopian(int n1, int n2) pure nothrow
in {
in {
assert(n1 >= 0, "muliplier cannot be negative");
assert(n1 >= 0, "Multiplier can't be negative");
} body {
} body {
static int doubleNum(in int n) pure nothrow { return n * 2; }
static enum doubleNum = (in int n) pure nothrow => n * 2;
static int halveNum(in int n) pure nothrow { return n / 2; }
static enum halveNum = (in int n) pure nothrow => n / 2;
static bool isEven(in int n) pure nothrow { return !(n & 1); }
static enum isEven = (in int n) pure nothrow => !(n & 1);


int result;
int result;
Line 945: Line 943:


return result;
return result;
} unittest {
}

unittest {
assert(ethiopian(77, 54) == 77 * 54);
assert(ethiopian(77, 54) == 77 * 54);
assert(ethiopian(8, 923) == 8 * 923);
assert(ethiopian(8, 923) == 8 * 923);
Line 954: Line 950:


void main() {
void main() {
import std.stdio;

writeln("17 ethiopian 34 is ", ethiopian(17, 34));
writeln("17 ethiopian 34 is ", ethiopian(17, 34));
}</lang>
}</lang>
{{out}}
Output:
<pre>17 ethiopian 34 is 578</pre>
<pre>17 ethiopian 34 is 578</pre>