Anonymous user
Ethiopian multiplication: Difference between revisions
→{{header|x86 assembly}}: ++ unix shell
(→{{header|R}}: another implementation using another approach (some languages may prefer this approach?)) |
(→{{header|x86 assembly}}: ++ unix shell) |
||
Line 565:
1 544 KEPT
17 * 34 = 578</pre>
=={{header|UNIX Shell}}==
(Tried with <tt>bash --posix</tt>, so it should run in <tt>sh</tt> too)
<lang bash>halve()
{
echo $(( $1 / 2 ))
}
double()
{
echo $(( $1 * 2 ))
}
iseven()
{
echo $(( $1 % 2 == 0 ))
}
ethiopicmult()
{
plier=$1
plicand=$2
r=0
while [ $plier -ge 1 ]; do
if [ $(iseven $plier) -eq 0 ]; then
r=$(( r + plicand))
fi
plier=$(halve $plier)
plicand=$(double $plicand)
done
echo $r
}
echo $(ethiopicmult 17 34)</lang>
=={{header|x86 assembly}}==
|