Here's a handy trick for adding (reps.,
multiplying) all the elements of a list.
To add all the squares of the
integers from
to
is easy:
> L:=[]; > for i in [1..10] do for> L:=Append(L,i^2); for> end for; > sum:=&+L; > sum; 385The
&+command iterates the
> product:=&*L; > product; 13168189440000
The number of elements in a list L is
obtained by typing the command #L;.
For example,
> L:=[]; > for i in [1..10] do for> L:=Append(L,i^2); for> end for; > #L;returns