Ethiopian multiplication: Difference between revisions
Content deleted Content added
No edit summary |
Ada |
||
Line 49: | Line 49: | ||
*[http://www.ncetm.org.uk/blogs/3064 Ethiopian multiplication] |
*[http://www.ncetm.org.uk/blogs/3064 Ethiopian multiplication] |
||
*[http://www.bbc.co.uk/dna/h2g2/A22808126 Russian Peasant Multiplication] |
*[http://www.bbc.co.uk/dna/h2g2/A22808126 Russian Peasant Multiplication] |
||
=={{header|Ada}}== |
|||
<lang Ada>package Ethiopian is |
|||
function Multiply(Left, Right : Integer) return Integer; |
|||
end Ethiopian;</lang> |
|||
<lang Ada>package body Ethiopian is |
|||
function Is_Even(Item : Integer) return Boolean is |
|||
begin |
|||
return Item mod 2 = 0; |
|||
end Is_Even; |
|||
function Double(Item : Integer) return Integer is |
|||
begin |
|||
return Item * 2; |
|||
end Double; |
|||
function Half(Item : Integer) return Integer is |
|||
begin |
|||
return Item / 2; |
|||
end Half; |
|||
function Num_Doubles(Item : Integer) return Natural is |
|||
Count : Natural := 0; |
|||
Temp : Integer := Item; |
|||
begin |
|||
loop |
|||
Count := Count + 1; |
|||
Temp := Half(Temp); |
|||
exit when not Is_Even(Temp); |
|||
end loop; |
|||
return Count; |
|||
end Num_Doubles; |
|||
function Multiply(Left, Right : Integer) return Integer is |
|||
Temp : Integer := Right; |
|||
begin |
|||
for I in 1..Num_Doubles(Left) loop |
|||
Temp := Double(Temp); |
|||
end loop; |
|||
Temp := Temp + Right; |
|||
return Temp; |
|||
end Multiply; |
|||
end Ethiopian;</lang> |
|||
<lang Ada>with Ethiopian; use Ethiopian; |
|||
with Ada.Text_Io; use Ada.Text_Io; |
|||
procedure Ethiopian_Test is |
|||
First : Integer := 17; |
|||
Second : Integer := 34; |
|||
begin |
|||
Put_Line(Integer'Image(First) & " times " & |
|||
Integer'Image(Second) & " = " & |
|||
Integer'Image(Multiply(First, Second))); |
|||
end Ethiopian_Test;</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |