I want to make the panjer model for the Poisson but my problem is that I do not know how to iterate for the previous ones ,ie for x=2 it comes out but for x=3 no, I need to save the result of x=2 to add it to x=3 and so on,here is the code I did.

poispanjer<-function(x,lamda){

if (x>0){

g<- numeric(x+1)

g[1]<-dpois(0,lamda)

g[2]<-lamda*dpois(1,lamda)*g[1]
gf<- numeric(x)
for (i in 1:x){
gf[x-i+1]<-(lamda*(x-i+1)/x)*dpois((x-i+1),lamda)*g[i]

}

g[i+1] <- sum(gf)

print(g[x+1])}

else {print(dpois(0,lamda))}

}