Ethiopian multiplication: Difference between revisions
Content added Content deleted
(J: renamed "even test" function so its name matches values where it is true) |
(→{{header|Bash}}: added Bash solution) |
||
Line 254: | Line 254: | ||
print ethiopian(17, 34) |
print ethiopian(17, 34) |
||
}</lang> |
}</lang> |
||
=={{header|Bash}}== |
|||
<lang bash>halve() { |
|||
local n=$1 |
|||
echo -n $(( n / 2 )) |
|||
} |
|||
double() { |
|||
local n=$1 |
|||
echo -n $(( n * 2 )) |
|||
} |
|||
is_even() { |
|||
local n=$1 |
|||
[ $(( n % 2 )) -eq 0 ] |
|||
} |
|||
multiply() { |
|||
local plier=$1 |
|||
local plicand=$2 |
|||
local result=0 |
|||
while [ $plier -gt 0 ] |
|||
do |
|||
! is_even $plier && (( result += plicand )) |
|||
plier=$( halve $plier ) |
|||
plicand=$( double $plicand ) |
|||
done |
|||
echo -n $result |
|||
} |
|||
echo $( multiply 17 34 )</lang> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |