This site is supported by donations to The OEIS Foundation.

User:Anders Hellström/Pari

From OeisWiki
Jump to: navigation, search

Pari/GP

See also contributions.


Pari/GP code

See also Talk page

divides(n,m)=(divrem(m,n)~[2])==0

divides(n,m)=my(r=m/n);r==floor(r) => divides(n,m)=m%n == 0

divides(n,r)=while(n<=r,r-=n);r==0

log10(n)=log(n)/log(10)

primeindex(n)=my(k=1);while(prime(k)<n,k++);k

istriangular(n)=ispolygonal(n,3) A000217

number_of_digits(n)=#Str(n) = A055642(n) [only for positive numbers]

number_of_digits(n)=if(n==0,return(1),return(truncate(log(abs(n))/log(10) + 1)))

digitsum(n)=my(d=digits(n,10),r=0);for(i=1,#d,r=r+d[i]);r alt builtin sumdigits(n) = A007953(n)

issemiprime(n)=bigomega(n)==2 A001358

vecprod(v,n)=my(p=1);for(i=1,n,p*=v[i]);p; [product up to n-th element of v]

prime1(n)=if(n==0,1,prime(n));

prime2(n)=if(n<=0,1,prime(n));

repunit(n)=my(r="",i);if(n>0,for(i=1,n,r=concat(r,"1")));eval(r); alt repunit(k)=R_n=(10^k-1)/9=A002275(k)

factors(k)=factor(k)[,1]~


[A006530 Gpf(n): greatest prime dividing n; a(1)=1.]

gpf(n)=my(f=factor(n)[, 1]);f[#f];

gpf(n)=my(f=factors(n));f[#f] alt gpf(n)=if(n>1, vecmax(factor(n)[, 1]), 1)

gpf(n) = if(n>1, vecmax(factor(n)[, 1]), 1); [Defined for n <= 1.]

gpf(n)=vecmax(factor(n)[, 1]))


spf(n)=factor(n)[1,1] = A020639(n)

spf(n)=my(i=0);until(n%prime(i)==0,i=i+1);prime(i)

gpfp(w)=my(t=#w);gpf(1+prod(i=1,t,w[i]));

A(m,n)=if(m,if(n,A(m-1, A(m,n-1)),A(m-1,1)),n+1); [ Ackerman function two argument version. See Talk page for an efficient version. ]

sumv(v)=my(j=0);for(i=1,#v,j+=v[i]);j; [summing vector = vecsum(v).]

n->n+1; n->n%5; n->0; [for apply()]

first(m)=vector(10, n, n--; f(n)) [to get f(0)]

expr(v)={my(l = #v,r=0);for(i=1,l,r += prime(i)^v[i];)}

a(m)=expr(factorsm(m)))

hyperfactorial(n)=prod(k=2,n,k^k)=A002109(n) {{Wikipedia:https://en.wikipedia.org/wiki/Hyperfactorial#Hyperfactorial%7CHyperfactorial}}

\\https://oeis.org/wiki/Omega%28n%29,_number_of_prime_factors_of_n_%28with_multiplicity%29 \\factorsm={change from matrix to vector: all factors with multiplicity} factorsm(n)=my(v=factor(n), f=factor(n)[, 1]~, w=[]); for(i=1, #f, for(j=1, v[i, 2], w=concat(w, f[i]))); w;

writef(m)=my(v=first(m)); for(i=1, m, write("file.txt", i, " ", v[i])); is_in(n,w)=my(t=0);for(i=1,#w,if(n==w[i],t=1;break));t;

[index of prime, if n is not a prime 0. Cf. A036234, A000720, primepi()] primeindex(n)=if(isprime(n),t=1;while(n>prime(t),t++);t,0)

indexof(n)=i=1;while(prime(i)<n,i++);i => indexof(n)=primepi(n-1)+1 \\Index of smallest prime >= n

first_EM(m,k)=my(v=vector(m),j=prime(k));v[1]=j;for(i=2,m,v[i]=gpf(1+prod(j=1,i-1,v[j])));v; [Euclid-Mullin initial value j]

first_EM(m,p)=my(v=vector(m),j=p);v[1]=j;for(i=2,m,v[i]=gpf(1+prod(j=1,i-1,v[j])));v;

show_EM(j,k)={for(i=1,j,print(first_EM(k,i)));} [Show some EM sequences.]

show_EM(j,k)={for(i=1,j,print(first_EM(k,i)));}

first_EM(m,k)=my(v=vector(m),j=prime(k));v[1]=j;for(i=2,m,v[i]=gpf(1+prod(j=1,i-1,v[j])));v;

digits_EM(k)=d=0;f=first_EM(8,k);for(i=1,#f,d+=#digits(f[i]));d;

show_digits_EM(j)=for(i=1,j,print1("(",i,")=>",digits_EM(i),", "))


for (n=1, 20000, d=floor(x); x=(x-d)*10; write("bxxxx.txt", n, " ", d)); [for constants.]


gpf(n)=my(f=factor(n)[, 1]);f[#f]; a(n)=if(n<3,1,a(n-1)+gpf(1+prod(i=1,n-2,a(i)))

Missing functionality in PARI/GP / suggestions

See also Talk page

Running forprime backwards: forprime(n=1000,10,...)

timelimit for (top) function:

? set_timelim(fn,2h)
? fn()

after 2h it prints out its state:

c=89; n=7 ... etc

and

? set_timelim(fn1,1h);set_timelim(fn2,30min);fn1();fn2()
randoms() {using system random generator}
randomh() {using hardware random generator}
a==b==c {a==b&&b==c}
a=b=c {a=c;b=c}
issemiprime(n)
clr() clearing screen (Cf. cls,clear)
clss() clearing session (clearing defs and/or variables)
repeat(statemnt) = while(1,..)
for(_=0,10,for(_=1,20,expr) Anonymous variable.
np={next provable prime} (Cf. np=nextprime(n+1))
gpf()
factorsm()
last(v) last element of vector = v[-1] and giving error instead of last elem for v[-1] to avoid hiding of errors in code.
break(:label)
multiple definitions on one line (c/lf tab indentation) (pasting whole program).(Cf. {{Wikipedia:https://en.wikipedia.org/wiki/IPython%7CIPython%7C}})
saving program/session to file.
=> writebin("file.dat") and default(histfile, 1)
vector(size,i=-1,some_expr(i))

vector(size,some_expr(i))=[some_expr(1)...some_expr(size)] => apply(some_expr, [start..end])

listing all userdefined functions. => ?0

globals() listing all user defined globals.

Links

See Links page