\\ Pari utilities and functions not in the origional distribution
\\ by Cino Hilliard Sept 8, 2004
\\
\\ Highlight and copy script below or copy the entire text for handy reference. Then paste into a blank
\\ util.gp file in a working directory. Include the line read "util" to the .gprc file. If you do not want to put
\\ this in the .gprc file then just read it in a session \r util. Now when you open gp you will have many
\\ useful functions listed below.
\\ String handling functions
\\ asc(chr) Get the ASCII code of character chr. except A - z, chr require double quotes.
\\ mid(str,s,n) Get a substring of length n from string, str starting at position s in str.
\\ rev(str) Get the reverse of the input string str.
\\ left(str,n) Get the left n characters from string str.
\\ right(str,n) Get the right n characters from string str.
\\ instr(str,m) Get the position of occurrence of match string, m in string str.
\\ countchr(str,chr) Get the number of occurrences of character, chr in string str.
\\ countmatch(str,m) Get the number of unique occurrences of match string, m in string str.
\\ countmatch2(str,m) Get the number of all occurrences of match string m, in string str.
\\
\\ Number theory functions
\\ Li(x) Logarithmic integral
\\ pi(n) Prime count function - number of primes less than or equal to n
\\ nexttwin(n) Next twin prime pair including or after n.
\\ pitwin(n) The number of twin primes less than n.
\\ twinl(n) The nth lower twin prime
\\ twinu(n) The nth upper twin prime
\\ composites(n) The number of composite numbers less than or equal to n
\\ composite(n) The n-th composite
\\ primorial(n) The product of the first n primes using forprime.
\\ primorial2(n) The product of the first n primes using isprime
\\ primorial3(n) The product of the first n primes using ispseudoprime
\\ nextsquare(n) The next square number greater than n.
\\ ifactor(n) The vector of the integer factors with multiplicity
\\ iscube(n) Return 1 if n is a perfect cube otherwise return 0
\\ ispower(n) Return 1 if n is a perfect power otherwise return 0
\\ isinteger(n) Return i if n is an integer
\\ ispal(n) Return 1 if n is a palindromic number
\\ base(r1,r2,n) Return n base r1 to base r2. bases 2 - 255 are allowed.
\\
\\Some comments and the actual code begins here
\\util.gp
\\ This utility file should be in the working directory
\\ add read "util" to the .gprc file
\\ However the pari ascii set is different from the dos set. Eg., code 227 = symbol
\\ for pi in dos. Here we get a.
\\ Enclose str,chr, in "" to avoid errors.
\\ You may have to use kill to clear the variable if you don't use the "".
acodes() = \\ Store the ascii codes globally. User will have to kill ascii if using in other scripts in the session.
{
local(x);
ascii=vector(255);
for(x=1,255,ascii[x]=Strchr(x));
}
acodes() \\ Define the Pari ascii table globally
Li(x) = \\ Logarithmic integral
{
-eint1(log(1/x))
}
pi(n) = \\pi(n) prime count function
{
local(c,x);
c=0;forprime(x=1,n,c++);return(c)
}
nexttwin(n) = \\ next twin prime pair including or after n.
{
local(x);
forprime(x=n,n+n,if(isprime(x+2),return(x)))
}
pitwin(n) = \\ the number of twin primes less than n.
{
local(x,c);
forprime(x=3,n,if(isprime(x+2),c++));
return(c)
}
twinl(n) = \\The nth lower twin prime
{
local(c,x);
c=0;
x=1;
while(c<N,
<N,
if(isprime(prime(x)+2),c++);
x++;
);
return(prime(x-1))
}
twinu(n) = \\The nth upper twin prime
{
local(c,x);
c=0;
x=1;
while(c<N,
<N,
<N,
if(isprime(prime(x)+2),c++);
x++;
);
return(prime(x))
}
composites(n) = \\ The number of composite numbers less than or equal to n
{
return(n-pi(n))
}
composite(n) = \\ the n-th composite
{
local(c,x);
c=1;
x=0;
while(c <= n,
x++;
if(!isprime(x),c++);
);
return(x)
}
asc(chr) =
{
setsearch(ascii,chr)
}
mid(str,s,n) = \\ Get a substring of length n from string str starting at position s in str.
{
local(v,ln,x,tmp);
v ="";
tmp = Vec(str);
ln=length(tmp);
for(x=s,s+n-1,
v=concat(v,tmp[x]);
);
return(v)
}
rev(str) = \\ Get the reverse of the input string
{
local(tmp,s,j);
tmp = Vec(Str(str));
s="";
forstep(j=length(tmp),1,-1,
s=concat(s,tmp[j]));
return(s)
}
left(str,n) = \\ Get the left n characters from string str.
{
local(v,tmp,x);
v ="";
tmp = Vec(str);
ln=length(tmp);
if(n > ln,n=ln);
for(x=1,n,
v=concat(v,tmp[x]);
);
return(v)
}
right(str,n) = \\ Get the right n characters from string str.
{
local(v,ln,s,x);
v ="";
tmp = Vec(str);
ln=length(tmp);
if(n > ln,n=ln);
s = ln-n+1;
for(x=s,ln,
v=concat(v,tmp[x]);
);
return(v)
}
instr(str,match) = \\ Get the position of occurrence of match string in string str.
{
local(lns,lnm,x);
str=Str(str); \\This allows leaving quotes off input
match=Str(match);
lnm=length(match);
lns=length(str);
for(x=1,lns-lnm+1,
if(mid(str,x,lnm)==match,return(x))
)
}
countchr(str,char) = \\Count the number of occurrences of char in string str
{
local(ln,x,c);
str=Str(str); \\This allows leaving quotes off input
char=Str(char);
c=0;
ln=length(str);
for(x=1,ln,
if(mid(str,x,1)==char,c++);
);
return(c)
}
countmatch(str,match) = \\Count the number of unique occurrences of string match in string str
{
local(lnm,lns,x,c);
str=Str(str); \\This allows leaving quotes off input
match=Str(match);
c=0;
i=0;
lns=length(str);
lnm=length(match);
if(lnm>1,i=1);
x=1;
while(x<=lns-lnm+1,
if(mid(str,x,lnm)== match,c++;x+=lnm,x++);
);
return(c)
}
countmatch2(str,match) = \\Count the number of all occurrences of string match in string str.
{
local(lnm,lns,x,c,i);
str=Str(str); \\This allows leaving quotes off input
match=Str(match);
c=0;
i=0;
lns=length(str);
lnm=length(match);
if(lnm>1,i=1);
x=1;
while(x<=lns-lnm+1,
if(mid(str,x,lnm)==match,c++);
x++;
);
return(c)
}
primorial(n) \\ Product of the first primes using forprime
{
local(p1,x);
p1=1;
forprime(x=2,n,p1*=x);
return(p1)
}
primorial2(n) = \\ Product of the first n primes using isprime proven primes
{
local(p1,x);
p1=2;
forstep(x=3,n,2,if(isprime(x),p1*=x));
return(p1)
}
primorial3(n) = \\ Product of the first n primes using ispseudoprime probable primes
{
local(p1,x);
p1=2;
forstep(x=3,n,2,if(ispseudoprime(x),p1*=x));
return(p1)
}
nextsquare(n) =
{
return((floor(sqrt(n))+1)^2)
}
ifactor(n) =
{
local(f,j,k,flist);
flist=[];
f=Vec(factor(n));
for(j=1,length(f[1]),
for(k = 1,f[2][j],flist = concat(flist,f[1][j])
);
);
return(flist)
}
iscube(n) =
{
local(r);
r = n^(1/3);
if(floor(r+.5)^3== n,1,0)
}
ispower(n) =
{
local(p,r,j);
r = sqrt(n);
for(j=2,floor(r),
p = floor(log(n)/log(j)+.5);
if(j^p ==n,return(1));
);
return(0)
}
isinteger(n) =
{
if(n==floor(n),return(1),return(0))
}
ispal(n) =
{
local(flag,len,v,i);
n=Str(n);
len=length(n);
v=Vec(n);
flag=1;
for(i=1,len\2,
if(v[i] <> v[len-i+1],flag=0;break)
);
return(flag)
getcodes() =
{
local(j,ct);
code = vector(257);
ct=0;
for(j=48,57,
ct++;
code[ct] = Strchr(j);
);
for(j=65,90,
ct++;
code[ct] = Strchr(j);
);
for(j=97,122,
ct++;
code[ct] = Strchr(j);
);
for(j=32,47,
ct++;
code[ct] = Strchr(j);
);
for(j=58,64,
ct++;
code[ct] = Strchr(j);
);
for(j=91,96,
ct++;
code[ct] = Strchr(j);
);
for(j=123,255,
ct++;
code[ct] = Strchr(j);
);
for(j=1,31,
ct++;
code[ct] = Strchr(j);
);
}
getcodes()
base(r1,r2,num) = \\This is the crown jewel of the routine
{
local(RDX,asci,Q,j,ln,p,i,x,d,c);
local(pwr,dec,dec2,Qq,k,lnq,r1q,r2q);
num=Str(num);
if(num == "0",return("0"));
d="";
RDX="";
r1q=r1;
r2q=r2;
dec=0;
ln = length(num);
for(j=1,ln,
c=0;
d = mid(num,j,1);
until(d==code[c],c++);
asci = c-1;
lnq = ln-j;
dec += asci*r1q^lnq; \\Convert the num in r1 to decimal
);
j=1;
while(mid(num,j,1)=="0",
RDX=concat(RDX,"0");
j++;
);
j=0;
dec2=dec;
while(dec2 > 0,
dec2 = floor(dec2/r2);
j++;
);
forstep(k=j-1,0,-1,
pwr = r2q^k; \\This is just going from base 10 to another base.
Q = floor(dec / pwr);\\converting the decimal number to r2 which in effect
dec = dec - pwr*Q; \\converting num in base r1 to a number in base r2.
RDX = concat(RDX,code[Q+1]); \\call globally declared code[] character base
\\ RDX = concat(RDX,Str(Q)); \\Do it like Maple except reverse order
\\ RDX = concat(RDX,","); \\ seperate by comma leave off brackets
);
return(RDX)
}