loda-lang / loda-cpp

Runtime and miner for the LODA language written in C++
https://loda-lang.org/
Apache License 2.0
21 stars 1 forks source link

Vector-based PARI output #316

Open ckrause opened 7 months ago

ckrause commented 7 months ago

For faster computation using PARI, use vectors to cache previously computed terms, e.g. for a(n)=if(n<2,1,a(n-1)+a(n-2)) use

a(n)=
{
  my(V=vector(n));
  V[1]=1; V[2]=1;
  for(i=3, n,
    V[i] = V[i-1] + V[i-2]
  );
  V;
}
ckrause commented 5 months ago

simple test for two vectors:

? V=vector(10)
%1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
? W=vector(10)
%2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
? V[1]=1
%3 = 1
? W[1]=1
%4 = 1
? for(i=2, 10, V[i]=V[i-1]+W[i-1]; W[i]=i)
? V
%6 = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46]
? W
%7 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ckrause commented 5 months ago

Preparation work done:

$ ./loda export -o pari-vector A58
(a[n] = b[n]+1); (b[n] = b[n-1]*(b[n-1]+1)); (b[0] = 1)