Ethiopian multiplication: Difference between revisions

Line 622:
 
=={{header|D}}==
Works with DMD V.2.051.
<lang d>import std.stdio: writeln;
 
pure nothrow int ethiopian(int n1, int n2)
in {
assert(n1 >= 0, "muliplier cannot be negative");
} body {
static pure nothrow int doubleNum(const int n) pure { return n * 2; }
static pure nothrow int halveNum(const int n) pure { return n / 2; }
static pure nothrow bool isEven(const int n) pure { return !(n % 2); }
 
int result;
 
while (n1 >= 1) {
if (!isEven(n1))
Line 640 ⟶ 641:
n2 = doubleNum(n2);
}
 
return result;
}
Anonymous user