How to use recursion to define a number series in maple

130 Views Asked by At

I am new to maple and I want to define a number series as follows, but the error code is that there are too many levels of iterations. How to solve this problem?

g:=proc(x) g(x):=(sum(g(i),i=1..x-1))/(x!) end proc: g(1):=0:

Thanks in advance!

1

There are 1 best solutions below

2
On

Are you after something like this:

g:=proc(x)
  option remember, system;
  local i;
  add(procname(i),i=1..x-1)/(x!);
end proc:

g(1):=1:

seq(g(n),n=1..6);

    1, 1/2, 1/4, 7/96, 35/2304, 847/331776

Inside the procedure, I could have written the recursive call as g(i), but then its functionlity is tied to the name to which you assign it. By using procname(i) you are free to assign the procedure to another name.

For a Maple procedure, having option remember is a kind of memoization. This speeds up this recursive procedure.

Having option system means that the stored results will get automatically cleared when Maple performs a garbage-collection cleanup. You could remove that, if you prefer.