Jump to content

Ethiopian multiplication: Difference between revisions

→‎{{header|dc}}: Add implementation
(→‎{{header|LOLCODE}}: Add LOLCODE)
(→‎{{header|dc}}: Add implementation)
Line 958:
Output:
<pre>17 ethiopian 34 is 578</pre>
 
=={{header|dc}}==
<lang dc>0k [ Make sure we're doing integer division ]sx
[ 2 / ] sH [ Define "halve" function in register H ]sx
[ 2 * ] sD [ Define "double" function in register D ]sx
[ 2 % 1 r - ] sE [ Define "even?" function in register E ]sx
 
[ Entry into the main Ethiopian multiplication function is register M ]sx
[ Keeps running value for the product in register p ]sx
[ 0 sp lLx lp ] sM
 
[ The body of the main loop is in register L ]sx
 
[
sb sa [ First thing we do is cheat and store the parameters in
registers, which is safe because the only recursion is of
the tail variety. This avoids tricky stack
manipulations, which dc doesn't have good support for
(unlike, say, Forth). ]sx
 
la lEx sr [ r = even?(a) ]sx
lr 0 =S [ if r = 0 then call s]sx
la lHx d [ a = halve(a)]sx
lb lDx [ b = double(b)]sx
r 0 !=L [ if a !=0 then recurse ]
] sL
 
[ Utility macro that just adds the current value of b to the total in p ]sx
[ lp lb + sp ]sS
 
[ Demo by multiplying 17 and 34 ]sx
17 34 lMx p</lang>
Output: <pre>578</pre>
 
=={{header|E}}==
1,481

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.