Ethiopian multiplication: Difference between revisions

octave
(R)
(octave)
Line 144:
 
end program EthiopicMult</lang>
 
=={{header|Octave}}==
<lang octave>function r = halve(a)
r = floor(a/2);
endfunction
 
function r = doublit(a)
r = a*2;
endfunction
 
function r = iseven(a)
r = mod(a,2) == 0;
endfunction
 
function r = ethiopicmult(plier, plicand, tutor=false)
r = 0;
if (tutor)
printf("ethiopic multiplication of %d and %d\n", plier, plicand);
endif
while(plier >= 1)
if ( iseven(plier) )
if (tutor)
printf("%4d %6d struck\n", plier, plicand);
endif
else
r = r + plicand;
if (tutor)
printf("%4d %6d kept\n", plier, plicand);
endif
endif
plier = halve(plier);
plicand = doublit(plicand);
endwhile
endfunction
 
disp(ethiopicmult(17, 34, true))</lang>
 
=={{header|Perl}}==