Question: How to sum all the numbers in a list L of numbers

I did two attempts with different ideas
 

> 

 

> 

restart;

Task (a) A list of numbers in Maple :

> 

L:=[1,2,3,4,5];

[1, 2, 3, 4, 5]

(1)
> 

 

In general summing up numbers sum(a, i = k .. n) = sum(a_i, i = 1 .. 5) and sum(a_i, i = 1 .. 5) = a1+a2+a3+a4+a5

underscript? ..i like to write a with underscore n : how to that

> 

#S:={seq(L[i],i=1..5)};

> 

#L:= [seq(L[i],i=1..5)];

For summing up numbers in a list of L i can use a ...

Sumlist := proc (L,N):  

> 

Sumlist:=proc(L,N)

> 

   a:=1;

> 

   for i from 1 to N do

> 

      a:=seq(L[i],i=1..5);

> 

   end do;

> 

end proc:

> 

 

Warning, (in Sumlist) `a` is implicitly declared local

 

Warning, (in Sumlist) `i` is implicitly declared local

 
> 

L:=[1,2,3,4,5];

[1, 2, 3, 4, 5]

(2)
> 

N:=3;

3

(3)
> 

Sumlist(L,N);

1, 2, 3, 4, 5

(4)
> 

 

And now to sum the number sequenze  from the procedure?

 

> 

restart;

> 

Sumlist:=proc(L,N)

> 

   a:=1;

> 

   for i from 1 to N do

> 

      a:=seq(L[i],i=1..5);
      #sum(a, i=1..5); # for this probably a second do loop nested ?

> 

   end do;

> 

end proc:

> 

 

Warning, (in Sumlist) `a` is implicitly declared local

 

Warning, (in Sumlist) `i` is implicitly declared local

 
> 

L:=[1,2,3,4,5];

[1, 2, 3, 4, 5]

(5)
> 

N:=3;

3

(6)
> 

Sumlist(L,N);

1, 2, 3, 4, 5

(7)
> 

 

> 

 

> 

 

> 

 

========================================================================

Also possible by? : a1 = 1, a2=a1+1, a3=a2+1, etc

> 

restart;

 

> 

Sumlist:=proc(L,N)

> 

   a:=1;

> 

   for i from 1 to N do

> 

      a:=L[i]+1;

> 

   end do;

> 

end proc:

> 

 

Warning, (in Sumlist) `a` is implicitly declared local

 

Warning, (in Sumlist) `i` is implicitly declared local

 
> 

L:=[1,2,3,4,5];

[1, 2, 3, 4, 5]

(8)
> 

N:=2;

2

(9)
> 

Sumlist(L,N);

3

(10)
> 

L[1];

1

(11)
> 

 

Try some things out, but summing up list ?


 

Download betounes_ex_set_2_task_5.mw

 

Please Wait...