Bill Allombert on Wed, 22 Mar 2023 09:49:49 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: defining a recurrence


On Wed, Mar 22, 2023 at 02:01:52AM +0100, Jean-Luc ARNAUD wrote:
> Hi all,
> 
> Looking in PariGP documentation, I don't find how to define a recurrence.
> 
> For example, let say I'd like to define the Factorial function F:
> 
> F(x+1)=F(x)*(x+1)
> 
> F(1)=1
> 
> How to define F, if possible?

You need to use the 'if' statement to separate cases:

F(x)=if(x==0, 1, F(x-1)*x);
apply(F,[1..5])
%24 = [1,2,6,24,120]

if you have an anoymous function, you can use self() to refer to itself:

apply(x->if(x==0, 1, self()(x-1)*x),[1..5])
%27 = [1,2,6,24,120]

Cheers,
Bill