Ethiopian multiplication: Difference between revisions
Content added Content deleted
(added J) |
(→{{header|Perl}}: ++ php) |
||
Line 315: | Line 315: | ||
print ethiopicmult(17,34, 1) . "\n";</lang> |
print ethiopicmult(17,34, 1) . "\n";</lang> |
||
=={{header|PHP}}== |
|||
<lang php><?php |
|||
function halve($x) |
|||
{ |
|||
return floor($x/2); |
|||
} |
|||
function double($x) |
|||
{ |
|||
return $x*2; |
|||
} |
|||
function iseven($x) |
|||
{ |
|||
return ($x % 2) == 0; |
|||
} |
|||
function ethiopicmult($plier, $plicand, $tutor) |
|||
{ |
|||
if ($tutor) { echo "ethiopic multiplication of $plier and $plicand\n"; } |
|||
$r = 0; |
|||
while($plier >= 1) { |
|||
if ( !iseven($plier) ) { $r += $plicand; } |
|||
if ($tutor) { |
|||
echo "$plier, $plicand " . (iseven($plier) ? "struck" : "kept") . "\n"; |
|||
} |
|||
$plier = halve($plier); |
|||
$plicand = double($plicand); |
|||
} |
|||
return $r; |
|||
} |
|||
echo ethiopicmult(17, 34, true) . "\n"; |
|||
?></lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |