Ethiopian multiplication: Difference between revisions

Content added Content deleted
Line 622: Line 622:


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

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 int doubleNum(int n) pure { return n * 2; }
static pure nothrow int doubleNum(const int n) { return n * 2; }
static int halveNum(int n) pure { return n / 2; }
static pure nothrow int halveNum(const int n) { return n / 2; }
static bool isEven(int n) pure { return !(n % 2); }
static pure nothrow bool isEven(const int n) { return !(n % 2); }

int result;
int result;

while (n1 >= 1) {
while (n1 >= 1) {
if (!isEven(n1))
if (!isEven(n1))
Line 640: Line 641:
n2 = doubleNum(n2);
n2 = doubleNum(n2);
}
}

return result;
return result;
}
}