Sum and product of an array: Difference between revisions

Content added Content deleted
m (→‎{{header|Ruby}}: fixed typo)
m (Fixed lang tags.)
Line 82: Line 82:
=={{header|APL}}==
=={{header|APL}}==
{{works with|APL2}}
{{works with|APL2}}
sum ← +/
<lang apl> sum ← +/
prod ← ×/
prod ← ×/
list ← 1 2 3 4 5
list ← 1 2 3 4 5
sum list
sum list
15
15
prod list
prod list
120
120</lang>
=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang applescript>set array to {1, 2, 3, 4, 5}
<lang applescript>set array to {1, 2, 3, 4, 5}
Line 208: Line 208:
=={{header|ColdFusion}}==
=={{header|ColdFusion}}==
Sum of an Array,
Sum of an Array,
<cfset Variables.myArray = [1,2,3,4,5,6,7,8,9,10]>
<lang cfm><cfset Variables.myArray = [1,2,3,4,5,6,7,8,9,10]>
<cfoutput>#ArraySum(Variables.myArray)#</cfoutput>
<cfoutput>#ArraySum(Variables.myArray)#</cfoutput></lang>


Product of an Array,
Product of an Array,
<cfset Variables.myArray = [1,2,3,4,5,6,7,8,9,10]>
<lang cfm><cfset Variables.myArray = [1,2,3,4,5,6,7,8,9,10]>
<cfset Variables.Product = 1>
<cfset Variables.Product = 1>
<cfloop array="#Variables.myArray#" index="i">
<cfloop array="#Variables.myArray#" index="i">
<cfset Variables.Product *= i>
<cfset Variables.Product *= i>
</cfloop>
</cfloop>
<cfoutput>#Variables.Product#</cfoutput>
<cfoutput>#Variables.Product#</cfoutput></lang>
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(let ((data #(1 2 3 4 5))) ; the array
<lang lisp>(let ((data #(1 2 3 4 5))) ; the array
Line 233: Line 233:
auto r = reduce!("a + b", "a * b")(0, 1, array); // 0 and 1 are seeds for corresponding functions
auto r = reduce!("a + b", "a * b")(0, 1, array); // 0 and 1 are seeds for corresponding functions
writefln("Sum: ", r._0); // Results are stored in a tuple
writefln("Sum: ", r._0); // Results are stored in a tuple
writefln("Product: ", r._1);
writefln("Product: ", r._1);</lang>
</lang>
=={{header|Delphi}}==
=={{header|Delphi}}==
<lang delphi>var
<lang delphi>var
Line 253: Line 252:
{{works with|XEmacs|version 21.5.21}}
{{works with|XEmacs|version 21.5.21}}


(setq array [1 2 3 4 5])
<lang lisp>(setq array [1 2 3 4 5])
(eval (concatenate 'list '(+) array))
(eval (concatenate 'list '(+) array))
(eval (concatenate 'list '(*) array))
(eval (concatenate 'list '(*) array))</lang>


=={{header|Erlang}}==
=={{header|Erlang}}==
Line 283: Line 282:
sum_tail(Tail, Head + Acc).</lang>
sum_tail(Tail, Head + Acc).</lang>
=={{header|Factor}}==
=={{header|Factor}}==
1 5 1 <range> [ sum . ] [ product . ] bi
<lang factor>1 5 1 <range> [ sum . ] [ product . ] bi
15 120
15 120
{ 1 2 3 4 } [ sum ] [ product ] bi
{ 1 2 3 4 } [ sum ] [ product ] bi
10 24
10 24</lang>
sum and product are defined in the sequences vocabulary:
sum and product are defined in the sequences vocabulary:
: sum ( seq -- n ) 0 [ + ] reduce ;
<lang factor>: sum ( seq -- n ) 0 [ + ] reduce ;
: product ( seq -- n ) 1 [ * ] reduce ;
: product ( seq -- n ) 1 [ * ] reduce ;</lang>


=={{header|Forth}}==
=={{header|Forth}}==
: third ( a b c -- a b c a ) 2 pick ;
<lang forth>: third ( a b c -- a b c a ) 2 pick ;
: reduce ( xt n addr cnt -- n' ) \ where xt ( a b -- n )
: reduce ( xt n addr cnt -- n' ) \ where xt ( a b -- n )
cells bounds do i @ third execute cell +loop nip ;
cells bounds do i @ third execute cell +loop nip ;


create a 1 , 2 , 3 , 4 , 5 ,
create a 1 , 2 , 3 , 4 , 5 ,

' + 0 a 5 reduce . \ 15
' + 0 a 5 reduce . \ 15
' * 1 a 5 reduce . \ 120
' * 1 a 5 reduce . \ 120</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==
Line 332: Line 331:
p = product . elems $ values</lang>
p = product . elems $ values</lang>
=={{header|IDL}}==
=={{header|IDL}}==
array = [3,6,8]
<lang idl>array = [3,6,8]
print,total(array)
print,total(array)
print,product(array)
print,product(array)</lang>


=={{header|J}}==
=={{header|J}}==


sum =: +/
<lang j>sum =: +/
product =: */
product =: */</lang>


For example:
For example:


sum 1 3 5 7 9 11 13
<lang j> sum 1 3 5 7 9 11 13
49
49
product 1 3 5 7 9 11 13
product 1 3 5 7 9 11 13
135135
135135

a=: 3 10 ?@$ 100 NB. random array
a=: 3 10 ?@$ 100 NB. random array
a
a
90 47 58 29 22 32 55 5 55 73
90 47 58 29 22 32 55 5 55 73
58 50 40 5 69 46 34 40 46 84
58 50 40 5 69 46 34 40 46 84
29 8 75 97 24 40 21 82 77 9
29 8 75 97 24 40 21 82 77 9

sum a
sum a
177 105 173 131 115 118 110 127 178 166
177 105 173 131 115 118 110 127 178 166
product a
product a
151380 18800 174000 14065 36432 58880 39270 16400 194810 55188
151380 18800 174000 14065 36432 58880 39270 16400 194810 55188

sum"1 a
sum"1 a
466 472 462
466 472 462
product"1 a
product"1 a
5.53041e15 9.67411e15 1.93356e15
5.53041e15 9.67411e15 1.93356e15</lang>


=={{header|Java}}==
=={{header|Java}}==
Line 741: Line 740:
=={{header|Standard ML}}==
=={{header|Standard ML}}==
===Arrays===
===Arrays===
<lang ocaml>(* ints *)
<lang sml>(* ints *)
val a = Array.fromList [1, 2, 3, 4, 5];
val a = Array.fromList [1, 2, 3, 4, 5];
Array.foldl op+ 0 a;
Array.foldl op+ 0 a;
Line 750: Line 749:
Array.foldl op* 1.0 a;</lang>
Array.foldl op* 1.0 a;</lang>
===Lists===
===Lists===
<lang ocaml>(* ints *)
<lang sml>(* ints *)
val x = [1, 2, 3, 4, 5];
val x = [1, 2, 3, 4, 5];
foldl op+ 0 x;
foldl op+ 0 x;
Line 784: Line 783:
From an internal variable, $IFS delimited:
From an internal variable, $IFS delimited:


sum=0
<lang bash>sum=0
prod=1
prod=1
list="1 2 3"
list="1 2 3"
for n in $list
for n in $list
do sum="$(($sum + $n))"; prod="$(($prod * $n))"
do sum="$(($sum + $n))"; prod="$(($prod * $n))"
done
done
echo $sum $prod
echo $sum $prod</lang>


From the argument list (ARGV):
From the argument list (ARGV):


sum=0
<lang bash>sum=0
prod=1
prod=1
for n
for n
do sum="$(($sum + $n))"; prod="$(($prod * $n))"
do sum="$(($sum + $n))"; prod="$(($prod * $n))"
done
done
echo $sum $prod
echo $sum $prod</lang>


From STDIN, one integer per line:
From STDIN, one integer per line:


sum=0
<lang bash>sum=0
prod=1
prod=1
while read n
while read n
do sum="$(($sum + $n))"; prod="$(($prod * $n))"
do sum="$(($sum + $n))"; prod="$(($prod * $n))"
done
done
echo $sum $prod
echo $sum $prod</lang>


{{works with|GNU bash|3.2.0(1)-release (i386-unknown-freebsd6.1)}}
{{works with|GNU bash|3.2.0(1)-release (i386-unknown-freebsd6.1)}}
From variable:
From variable:


LIST='20 20 2';
<lang bash>LIST='20 20 2';
SUM=0; PROD=1;
SUM=0; PROD=1;
for i in $LIST; do
for i in $LIST; do
SUM=$[$SUM + $i]; PROD=$[$PROD * $i];
SUM=$[$SUM + $i]; PROD=$[$PROD * $i];
done;
done;
echo $SUM $PROD
echo $SUM $PROD</lang>
=={{header|UnixPipes}}==
=={{header|UnixPipes}}==
prod() {
<lang bash>prod() {
(read B; res=$1; test -n "$B" && expr $res \* $B || echo $res)
(read B; res=$1; test -n "$B" && expr $res \* $B || echo $res)
}
}


sum() {
sum() {
(read B; res=$1; test -n "$B" && expr $res + $B || echo $res)
(read B; res=$1; test -n "$B" && expr $res + $B || echo $res)
}
}


fold() {
fold() {
(func=$1; while read a ; do ; fold $func | $func $a done)
(func=$1; while read a ; do ; fold $func | $func $a done)
}
}




(echo 3; echo 1; echo 4;echo 1;echo 5;echo 9) | tee >(fold sum) >(fold prod) > /dev/null
(echo 3; echo 1; echo 4;echo 1;echo 5;echo 9) | tee >(fold sum) >(fold prod) > /dev/null</lang>


=={{header|Ursala}}==
=={{header|Ursala}}==
Line 846: Line 845:


=={{header|V}}==
=={{header|V}}==
[sp dup 0 [+] fold 'product=' put puts 1 [*] fold 'sum=' put puts].
<lang v>[sp dup 0 [+] fold 'product=' put puts 1 [*] fold 'sum=' put puts].</lang>
Using it
Using it
[1 2 3 4 5] sp
<lang v>[1 2 3 4 5] sp
=
=
product=15
product=15
sum=120
sum=120</lang>


=={{header|XSLT}}==
=={{header|XSLT}}==
XSLT (or XPath rather) has a few built-in functions for reducing from a collection, but product is not among them. Because of referential transparency, one must resort to recursive solutions for general iterative operations upon collections. The following code represents the array by numeric values in <price> elements in the source document.
XSLT (or XPath rather) has a few built-in functions for reducing from a collection, but product is not among them. Because of referential transparency, one must resort to recursive solutions for general iterative operations upon collections. The following code represents the array by numeric values in <price> elements in the source document.


<lang xml><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<pre><nowiki>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:output method="text" />
Line 892: Line 890:
</xsl:call-template>
</xsl:call-template>
</xsl:template>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet></lang>
</nowiki></pre>