Ethiopian multiplication: Difference between revisions

Content added Content deleted
(Use separate functions for doubling, halving, and checking if a number is even)
Line 1,451: Line 1,451:
578
578
=={{header|Seed7}}==
=={{header|Seed7}}==
{{incorrect|Seed7|The task specifies that examples should have separate functions for doubling, halving, and checking if a number is even.}}
Ethiopian Multiplication is another name for the peasant multiplication:
Ethiopian Multiplication is another name for the peasant multiplication:


<lang seed7>const func integer: peasantMult (in var integer: a, in var integer: b) is func
<lang seed7>const proc: double (in var integer: a) is func
begin
a *:= 2;
end func;

const proc: halve (in var integer: a) is func
begin
a := a div 2;
end func;

const func boolean: even (in integer: a) is
return not odd(a);

const func integer: peasantMult (in var integer: a, in var integer: b) is func
result
result
var integer: result is 0;
var integer: result is 0;
begin
begin
while a <> 0 do
while a <> 0 do
if odd(a) then
if not even(a) then
result +:= b;
result +:= b;
end if;
end if;
a := a div 2;
halve(a);
b *:= 2;
double(b);
end while;
end while;
end func;</lang>
end func;</lang>


Original source: [http://seed7.sourceforge.net/algorith/math.htm#peasantMult]
Original source (without separate functions for doubling, halving, and checking if a number is even): [http://seed7.sourceforge.net/algorith/math.htm#peasantMult]


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==