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 ← ×/ |
|||
list ← 1 2 3 4 5 |
|||
sum list |
|||
15 |
|||
prod list |
|||
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, |
||
<lang cfm><cfset Variables.myArray = [1,2,3,4,5,6,7,8,9,10]> |
|||
<cfoutput>#ArraySum(Variables.myArray)#</cfoutput></lang> |
|||
Product of an Array, |
Product of an Array, |
||
<lang cfm><cfset Variables.myArray = [1,2,3,4,5,6,7,8,9,10]> |
|||
<cfset Variables.Product = 1> |
|||
<cfloop array="#Variables.myArray#" index="i"> |
|||
<cfset Variables.Product *= i> |
|||
</cfloop> |
|||
<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}} |
||
<lang lisp>(setq array [1 2 3 4 5]) |
|||
(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}}== |
||
<lang factor>1 5 1 <range> [ sum . ] [ product . ] bi |
|||
15 120 |
|||
{ 1 2 3 4 } [ sum ] [ product ] bi |
|||
10 24</lang> |
|||
sum and product are defined in the sequences vocabulary: |
sum and product are defined in the sequences vocabulary: |
||
<lang factor>: sum ( seq -- n ) 0 [ + ] reduce ; |
|||
: product ( seq -- n ) 1 [ * ] reduce ;</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth>: third ( a b c -- a b c a ) 2 pick ; |
|||
: reduce ( xt n addr cnt -- n' ) \ where xt ( a b -- n ) |
|||
cells bounds do i @ third execute cell +loop nip ; |
|||
create a 1 , 2 , 3 , 4 , 5 , |
|||
' + 0 a 5 reduce . \ 15 |
|||
' * 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}}== |
||
<lang idl>array = [3,6,8] |
|||
print,total(array) |
|||
print,product(array)</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
<lang j>sum =: +/ |
|||
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 |
|||
product 1 3 5 7 9 11 13 |
|||
135135 |
|||
a=: 3 10 ?@$ 100 NB. random array |
|||
a |
|||
90 47 58 29 22 32 55 5 55 73 |
|||
58 50 40 5 69 46 34 40 46 84 |
|||
29 8 75 97 24 40 21 82 77 9 |
|||
sum a |
|||
177 105 173 131 115 118 110 127 178 166 |
|||
product a |
|||
151380 18800 174000 14065 36432 58880 39270 16400 194810 55188 |
|||
sum"1 a |
|||
466 472 462 |
|||
product"1 a |
|||
5.53041e15 9.67411e15 1.93356e15</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 741: | Line 740: | ||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
===Arrays=== |
===Arrays=== |
||
<lang |
<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 |
<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: |
||
<lang bash>sum=0 |
|||
prod=1 |
|||
list="1 2 3" |
|||
for n in $list |
|||
do sum="$(($sum + $n))"; prod="$(($prod * $n))" |
|||
done |
|||
echo $sum $prod</lang> |
|||
From the argument list (ARGV): |
From the argument list (ARGV): |
||
<lang bash>sum=0 |
|||
prod=1 |
|||
for n |
|||
do sum="$(($sum + $n))"; prod="$(($prod * $n))" |
|||
done |
|||
echo $sum $prod</lang> |
|||
From STDIN, one integer per line: |
From STDIN, one integer per line: |
||
<lang bash>sum=0 |
|||
prod=1 |
|||
while read n |
|||
do sum="$(($sum + $n))"; prod="$(($prod * $n))" |
|||
done |
|||
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: |
||
<lang bash>LIST='20 20 2'; |
|||
SUM=0; PROD=1; |
|||
for i in $LIST; do |
|||
SUM=$[$SUM + $i]; PROD=$[$PROD * $i]; |
|||
done; |
|||
echo $SUM $PROD</lang> |
|||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |
||
<lang bash>prod() { |
|||
(read B; res=$1; test -n "$B" && expr $res \* $B || echo $res) |
|||
} |
|||
sum() { |
|||
(read B; res=$1; test -n "$B" && expr $res + $B || echo $res) |
|||
} |
|||
fold() { |
|||
(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</lang> |
|||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 846: | Line 845: | ||
=={{header|V}}== |
=={{header|V}}== |
||
<lang v>[sp dup 0 [+] fold 'product=' put puts 1 [*] fold 'sum=' put puts].</lang> |
|||
Using it |
Using it |
||
<lang v>[1 2 3 4 5] sp |
|||
= |
|||
product=15 |
|||
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. |
||
⚫ | |||
<pre><nowiki> |
|||
⚫ | |||
<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> |