Rhys Terry
Rhys Terry

Reputation: 31

Maple Factorials

I'm trying to create a procedure in Maple 12 that takes an input "n" and outputs all the numbers in the sequence 1!, 1! + 2!, 1! + 2! + 3!, ... etc up to the user input and one value over (i.e the first number in the sequence greater than n).

Here is my current code:

 fact := proc (n);
 local i, a;
 a = 1
 i = 1
 while i < n do
 list(a) = i;
 a = a + 1
 i = b(1..(a! + list(a - 1)));
 od
 return(b)
 end

I'm almost completly new to Maple so the code is probably rubbish and haven't been given much information to work with, so anything to help would be gladly accepted.

Many Thanks

Upvotes: 0

Views: 199

Answers (1)

acer
acer

Reputation: 7271

Here are a few ways.

restart:

fact:=proc(n::posint)
  local i, a, b;
  a := 1;
  b[1] := 1;
  for i from 2 to n+1 do
    a := a*i;
    b[i] := b[i-1] + a;
  end do;
  return seq(b[i],i=1..n+1);
end proc: 

for k from 1 to 10 do
  fact(k);
end do;

restart:

fact2:=proc(n::posint)
  local i;
  seq(add(i!,i=1..k),k=1..n+1);
end proc:

for k from 1 to 10 do
  fact2(k);
end do;

restart:

fact3:=proc(n::posint)
  option rememeber;
  local i,t;
  if n=1 then
    return 1,3;
  else
    t:=fact3(n-1);
    return t, t[-1]+(n+1)!;
  end if;
end proc:

for k from 1 to 10 do
  fact3(k);
end do;

The second approach above is the least efficient, as it recomputes the factorials over and over, without re-use. The third approach is recursive.

Upvotes: 0

Related Questions