Ethiopian multiplication: Difference between revisions

Content added Content deleted
(AutoIt comes AFTER AutoHotkey :-) .)
Line 145: Line 145:
578
578
</pre>
</pre>

=={{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 := 0
While (a >= 1) {
if !isEven(a)
r += b
a := half(a)
b := double(b)
}
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|AutoIt}}==
=={{header|AutoIt}}==
Line 188: Line 220:
MsgBox(0, "Ethiopian multiplication of 17 by 34", Ethiopian(17, 34) )
MsgBox(0, "Ethiopian multiplication of 17 by 34", Ethiopian(17, 34) )
</lang>
</lang>

=={{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 := 0
While (a >= 1) {
if !isEven(a)
r += b
a := half(a)
b := double(b)
}
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}}==