Ethiopian multiplication: Difference between revisions

Content added Content deleted
(→‎{{header|GW-BASIC}}: Marked incorrect)
(Updated D code)
Line 705: Line 705:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio;
Works with DMD V.2.051.
<lang d>import std.stdio: writeln;


pure nothrow int ethiopian(int n1, int n2)
pure nothrow int ethiopian(int n1, int n2)
in {
in {
assert(n1 >= 0, "muliplier cannot be negative");
assert(n1 >= 0, "muliplier cannot be negative");
} body {
} body {
static pure nothrow int doubleNum(const int n) { return n * 2; }
static pure nothrow int doubleNum(in int n) { return n * 2; }
static pure nothrow int halveNum(const int n) { return n / 2; }
static pure nothrow int halveNum(in int n) { return n / 2; }
static pure nothrow bool isEven(const int n) { return !(n % 2); }
static pure nothrow bool isEven(in int n) { return !(n % 2); }


int result;
int result;
while (n1 >= 1) {
if (!isEven(n1))
result += n2;
n1 = halveNum(n1);
n2 = doubleNum(n2);
}


return result;
while (n1 >= 1) {
}
if (!isEven(n1))
result += n2;
n1 = halveNum(n1);
n2 = doubleNum(n2);
}

return result;
}


unittest {
unittest {