Ethiopian multiplication: Difference between revisions

Line 942:
return ab
}</lang>
=={{header|Eiffel}}==
<lang Eiffel>
class
ETHIOPIAN_MULTIPLICATION
feature{NONE}
double_int(n: INTEGER): INTEGER
do
Result:= n*2
end
 
halve_int(n: INTEGER) : INTEGER
do
Result:= n//2
end
 
even_int(n: INTEGER): BOOLEAN
do
Result:= n\\2=0
end
feature
ethiopian_mult(a,b: INTEGER): INTEGER
require
a_positive: a>0
b_positive: b>0
local
new_a, new_b: INTEGER
do
new_a:= a
new_b:= b
from
until
new_a<=0
loop
if even_int(new_a)= FALSE then
Result:= Result+new_b
end
new_a:= halve_int(new_a)
new_b:= double_int(new_b)
end
ensure
Result_correct: Result= a*b
end
end
</lang>
Test:
<lang Eiffel>
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
local
do
create mult
io.put_integer ( mult.ethiopian_mult (17,34))
end
mult: ETHIOPIAN_MULTIPLICATION
end
</lang>
{{out}}
<pre>
578
</pre>
 
=={{header|Emacs Lisp}}==
Emacs Lisp has <code>evenp</code> in cl.el (its Common Lisp library), but for the sake of completeness the desired effect is achieved here via <code>mod</code>.
Anonymous user