Product of Array: Difference between revisions
m (→[[PHP]]: grammatical changes) |
|||
Line 208: | Line 208: | ||
reduce(lambda z, y: z * y, x) |
reduce(lambda z, y: z * y, x) |
||
or |
|||
from operator import mul |
|||
reduce(mul, x) |
|||
'''Note:''' It is encouraged not to use this as reduce may go away in the future. |
'''Note:''' It is encouraged not to use this as reduce may go away in the future. |
Revision as of 13:23, 3 March 2007
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Compute the product of the elements of an Array
Ada
Define an unconstrained array type with an index starting at any valid integer value
type My_Array is array(Integer range <>) of Integer;
Define the product function
function Product(Item : My_Array) return Integer is Prod : Integer := 1; begin for I in Item'range loop Prod := Prod * Item(I); end loop; return Prod; end Product;
This function will raise the pre-defined exception Constraint_Error if the product overflows the values represented by type Integer
AppleScript
set array to {1, 2, 3, 4, 5} set product to 1 repeat with i in array -- very important -- list index starts at 1 not 0 set product to product * i end repeat
BASIC
10 REM Create an array with some test data in it 20 DIM ARRAY(5) 30 FOR I = 1 TO 5: READ ARRAY(I): NEXT I 40 DATA 1, 2, 3, 4, 5 50 REM Find the sum of elements in the array 60 PROD = 1 70 FOR I = 1 TO 5: PROD = PROD * ARRAY(I): NEXT I 80 PRINT "The product is "; PROD
C#
int value = 1; int[] arg = { 1,2,3,4,5 }; int arg_length = arg.Length; for( int i = 0; i < arg_length; i++ ) value *= arg[i];
Alternate
int prod = 1; int[] arg = { 1, 2, 3, 4, 5 }; foreach (int value in arg) prod *= value;
C++
int value = 1; int arg[] = { 1,2,3,4,5 }; int arg_length = sizeof(arg)/sizeof(arg[0]); int *end = arg+arg_length; for( int *p = arg; p!=end; ++p ) value *= *p;
Using the standard library:
#include <numeric> #include <functional> int arg[] = { 1, 2, 3, 4, 5 }; int value = std::accumulate(arg, arg+5, 1, std::multiplies<int>());
D
auto value = 1; auto array = [1, 2, 3, 4, 5]; foreach(v; array) value *= v;
Forth
: product ( addr cnt -- n ) 1 -rot cells bounds do i @ * cell +loop ;
FreeBASIC
dim array(4) as integer = { 1, 2, 3, 4, 5 } dim product as integer = 1 for index as integer = lbound(array) to ubound(array) product *= array(index) next
Haskell
let x = [1..10] product x -- the easy way foldl (*) 1 x -- the hard way
IDL
array = [3,6,8] print,product(array)
Java
int[] arg = { 1,2,3,4,5 }; int value = 1; for (int i: arg) value *= arg;
JavaScript
Interpreter: Firefox 2.0
var array = [1, 2, 3, 4, 5]; var product = 1; for( var i in array ) { product *= array[i]; } alert( product );
LISP
Interpreter: XEmacs (beta) version 21.5.21 of February 2005 (+CVS-20050720)
(setq array [1 2 3 4 5]) (eval (concatenate 'list '(*) array))
Interpreter: CLisp (ANSI Common Lisp)
(setq data '(1 2 3 4 5)) (apply #'* data)
OCaml
let x = [|1; 2; 3; 4; 5; 6; 7; 8; 9; 10|];; Array.fold_left ( * ) 1 x
Variant, using a liked list rather than an array:
let x = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];; List.fold_left ( * ) 1 x
PHP
$array = array(1, 2, 3); $product = 1; foreach($array as $key) { $product *= $key; } echo($product);
There is also an array product function built-in to PHP:
$array = array(1,2,3); echo array_product($array);
Perl
Interpeter: Perl
my $var; my @list = (1, 2, 3); map($var *= $_, @list);
Alternate
my $var; my @list = (1, 2, 3); $var *= $_ for (@list);
Prolog
product([],1). product([H|T],X) :- product(T,Y), X is H * X.
test
:- product([1,2,3,4,5],X). X = 120;
Python
Interpreter: Python 2.5
x = [1, 2, 3, 4, 5, 6, 7, 8, 9] product = 1 for i in x: product *= i
or
reduce(lambda z, y: z * y, x)
or
from operator import mul reduce(mul, x)
Note: It is encouraged not to use this as reduce may go away in the future.
Ruby
ary = [1,2,3,4,5] # or ary = *1..5 product = ary.inject{ |currentProduct, element| currentProduct * element } # => 120
Scala
val a = Array(1,2,3,4,5) a.foldLeft(1){(x,y) => x * y}
It may also be done in a classic imperative way :
var product = 1 for (val x <- a) product = product * x
Scheme
(define x '(1 2 3 4 5)) (apply * x)
A recursive solution, without the n-ary operator "trick":
(define (reduce f u L) (if (null? L) u (f (car L) (reduce f u (cdr L))))) (reduce * 1 '(1 2 3 4 5)) ;; 1 is unit for *
Standard ML
val x = [1,2,3,4,5] foldl (op*) 1 x
Tcl
set arr [list 3 6 8] set result [expr [join $arr *]]