Ethiopian multiplication: Difference between revisions
Content added Content deleted
(add AutoHotkey) |
|||
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|AutoHotkey}}== |
|||
<lang AutoHotkey>MsgBox % Ethiopian(17, 34) "`n" Ethiopian2(17, 34) |
|||
; func definitions: |
|||
half( x ) { |
|||
return x >> 1 |
|||
} |
|||
double( x ) { |
|||
return x << 1 |
|||
} |
|||
isEven( x ) { |
|||
return x & 1 == 0 |
|||
} |
|||
Ethiopian( a, b ) { |
|||
r := (isEven(a) ? 0 : b) |
|||
Loop |
|||
{ |
|||
a := half(a) |
|||
b := double(b) |
|||
if !isEven(a) |
|||
r += b |
|||
if (a = 1) |
|||
break |
|||
} |
|||
return r |
|||
} |
|||
; or a recursive function: |
|||
Ethiopian2( a, b, r = 0 ) { ;omit r param on initial call |
|||
return a==1 ? r+b : Ethiopian2( half(a), double(b), !isEven(a) ? r+b : r ) |
|||
}</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |