Question: How to create a polynomial P1, P2, ..., Pn = P sequentially using asignment x-> P + Pi with initial P = P1?

Hi, I am trying to run the following command in a Maple worksheet

P:= 1979 #(this is the1000-th prime);
S:=5000; k:=5:
a:=[0$i=1..k];
f:=[0$i=1..k];
a[1]:=S;
f:=x->a[1];
with(RandomTools):
for i from 2 to k do
a[i]:=Generate(integer(range=1..P-1));
f:=x->f(x)+a[i]*x^(k-1);
print(f(x));
od;

(This is a beginning step to create the Shamir's secret sharing).
However, I get the following output (with explanations)

                              5000          (This is correct. the value a[1])
x -> a[1]                                   (This should be the initial function f = 5000)
                              7005          (This is correct, the value a[2])
x -> f(x) + a[i] x^(k-1)               (one of the main problems, see below)

The main problems are in the last output above:
1. The f(x) is not as it should be: f(x) = a[1] = 5000;
2. The index i isn't changed when it should be i = 2;
3.  An error message: too many levels of recursion  

The output (I expect) should be: 5000 + 7005x, ...., etc.

Thanks in advanced for any help.

Please Wait...