Anonymous user
Ethiopian multiplication: Difference between revisions
m
Fixed lang tags.
m (→{{header|Ruby}}: move tests for clarity) |
m (Fixed lang tags.) |
||
Line 106:
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - missing printf and FORMAT}} -->
<lang algol68>PROC halve = (REF INT x)VOID: x := ABS(BIN x SHR 1);▼
<lang algol>▼
▲PROC halve = (REF INT x)VOID: x := ABS(BIN x SHR 1);
PROC doublit = (REF INT x)VOID: x := ABS(BIN x SHL 1);
PROC iseven = (#CONST# INT x)BOOL: NOT ODD x;
Line 135 ⟶ 134:
(
printf(($g(0)l$, ethiopian(17, 34, TRUE)))
</lang>▼
Output:
<pre>
Line 254 ⟶ 252:
Here is such an implementation without tutor, since there is no mechanism in C++ to output
messages during program compilation.
<lang cpp>template<int N>
struct Half
{
Line 308 ⟶ 305:
std::cout << EthiopianMultiplication<17, 54>::Result << std::endl;
return 0;
▲}</lang>
}▼
</lang>▼
=={{header|C sharp|C#}}==
<lang csharp>static void Main(string[] args)
{
EthopianMultiplication(17, 34);
Line 357 ⟶ 352:
Version with as a function of functions:
<lang cfm><cffunction name="double">▼
▲<cffunction name="double">
<cfargument name="number" type="numeric" required="true">
<cfset answer = number * 2>
Line 392 ⟶ 386:
<cfoutput>#ethiopian(17,34)#</cfoutput></lang>
</lang>▼
Version with display pizza:
<lang
<cfset Number_B = 34>
<cfset Result = 0>
Line 454 ⟶ 446:
...equals #Result#
</cfoutput></lang>
</lang>▼
Sample output:
Line 470 ⟶ 461:
=={{header|Clojure}}==
<lang
(bit-shift-right n 1))
Line 495 ⟶ 485:
(if (even a)
(recur (halve a) (twice b) r)
(recur (halve a) (twice b) (+ r b))))))</lang>
=={{header|Common Lisp}}==
Line 512 ⟶ 501:
=={{header|D}}==
<lang d>import std.stdio;
int dub(int num) { return num << 1; }
int halve(int num) { return num >> 1; }
Line 531 ⟶ 519:
writefln("17 ethiopian 34 is %d", ethiopian(17, 34));
return 0;
▲}</lang>
=={{header|E}}==
Line 550 ⟶ 537:
=={{header|FALSE}}==
<lang false>[2/]h:
[2*]d:
[1&]o:
Line 650 ⟶ 636:
'''Solution''':
<lang
'''Example''':
<lang j> 17 ethiop 34
Note: this could be implemented more concisely as <tt>#.@(*#:)</tt>, which abides by the letter of the task, but [http://www.jsoftware.com/pipermail/programming/2009-July/015507.html evades its spirit].
Line 702 ⟶ 688:
}</lang>
An optimised variant using the three helper functions from the other example.
<lang java5>/**
▲ * This method will use ethiopian styled multiplication.
▲ * @param b Any integer.
*/
▲ * @result a multiplied by b
if(a==0 || b==0) {
▲ public static int ethiopianMultiply(int a, int b) {
return 0;▼
int result = 0;
while(a>=1) {
}
▲ if(!isEven(a)) {
▲ result+=b;
▲ }
return result;▼
}
▲}
int result = 0;
while(a>=1) {
}
▲ if(!isEven(a)) {
▲ result+=b;
▲ }
}
▲}</lang>
== {{header|JavaScript}} ==
<lang javascript>var eth = {
halve : function ( n ){ return Math.floor(n/2); },
Line 787 ⟶ 770:
}
// eth.mult(17,34) returns 578</lang>
=={{header|Logo}}==
<lang logo>to double :x
output ashift :x 1
end
Line 806 ⟶ 787:
[output eproduct halve :x double :y] ~
[output :y + eproduct halve :x double :y]
end</lang>
=={{header|Metafont}}==
Line 1,111 ⟶ 1,091:
=={{header|PL/SQL}}==
This code was taken from the ADA example above - very minor differences.
<lang plsql>create or replace package ethiopian is
function multiply
Line 1,171 ⟶ 1,150:
dbms_output.put_line(ethiopian.multiply(17, 34));
end;
▲/</lang>
Line 1,180 ⟶ 1,158:
Everything encapsulated in one function.
<lang plpgsql>CREATE OR REPLACE FUNCTION ethiopian_multiplication(multiplier int, multiplicant int) RETURNS int AS $BODY$▼
▲CREATE OR REPLACE FUNCTION ethiopian_multiplication(multiplier int, multiplicant int) RETURNS int AS $BODY$
-- works at least on PostgreSQL 8.3, should work in older versions as well
DECLARE
Line 1,216 ⟶ 1,193:
RETURN sum;
END;
$BODY$ LANGUAGE 'plpgsql';</lang>
<lang plpgsql>SELECT ethiopian_multiplication(17, 34);</lang>▼
▲SELECT ethiopian_multiplication(17, 34);
<lang
NOTICE: 8, 68 STRIKE
NOTICE: 4, 136 STRIKE
Line 1,232 ⟶ 1,205:
--------------------------
578
(1 row)</lang>
=={{header|PowerShell}}==
<lang PowerShell>function isEven {
param ([int]$value)
return [bool]($value % 2 -eq 0)
Line 1,271 ⟶ 1,242:
}
multiplyValues 17 34</lang>
=={{header|Python}}==
<lang python>tutor = True
def halve(x):
Line 1,303 ⟶ 1,272:
multiplicand = double(multiplicand)
if tutor: print()
return result</lang>
Sample output
Line 1,322 ⟶ 1,290:
Without the tutorial code, and taking advantage of Python's lambda:
<lang python>halve = lambda x: x // 2
double = lambda x: x*2
even = lambda x : not x % 2
Line 1,336 ⟶ 1,303:
multiplicand = double(multiplicand)
return result</lang>
=={{header|R}}==
Line 1,511 ⟶ 1,477:
=={{header|SNUSP}}==
<lang snusp> /==!/==atoi==@@@-@-----#
| | /-\ /recurse\ #/?\ zero
$>,@/>,@/?\<=zero=!\?/<=print==!\@\>?!\@/<@\.!\-/
Line 1,522 ⟶ 1,487:
\>+<-/ | \=<<<!/====?\=\ | double
! # | \<++>-/ | |
\=======\!@>============/!/</lang>
This is possibly the smallest multiply routine so far discovered for SNUSP.
Line 1,608 ⟶ 1,572:
functions are normally imported from the nat library but defined here explicitly for
the sake of completeness.
<lang Ursala>odd = ~&ihB
double = ~&iNiCB
half = ~&itB</lang>
The functions above are defined in terms of bit manipulations exploiting the concrete representations
of natural numbers. The remaining code treats natural numbers instead as abstract types by way of the library API, and uses the operators for distribution (*-), triangular iteration (|\),
and filtering (*~) among others.
<lang Ursala>#import nat
emul = sum:-0@rS+ odd@l*~+ ^|(~&,double)|\+ *-^|\~& @iNC ~&h~=0->tx :^/half@h ~&</lang>
test program:
<lang Ursala>#cast %n
test = emul(34,17)</lang>
output:
<pre>
|