Ethiopian multiplication: Difference between revisions

Updated D entry
(Updated D entry)
Line 926:
 
=={{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 {
assert(n1 >= 0, "muliplierMultiplier cannotcan't be negative");
} body {
static intenum doubleNum = (in int n) pure nothrow { return=> n * 2; }
static intenum halveNum = (in int n) pure nothrow { return=> n / 2; }
static boolenum isEven = (in int n) pure nothrow { return=> !(n & 1); }
 
int result;
Line 945 ⟶ 943:
 
return result;
} unittest {
}
 
unittest {
assert(ethiopian(77, 54) == 77 * 54);
assert(ethiopian(8, 923) == 8 * 923);
Line 954 ⟶ 950:
 
void main() {
<lang d> import std.stdio;
 
writeln("17 ethiopian 34 is ", ethiopian(17, 34));
}</lang>
{{out}}
Output:
<pre>17 ethiopian 34 is 578</pre>