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)
}
diffdigits(n) = \\ Return 1 if all digits of n are different otherwise return 0
{
local(a,i,j,ln,f=1);
ln = length(Str(n));
a=eval(Vec(Str(n)));
for(i=1,ln,
for(j=1,ln,
if(i!=j&&a[i]==a[j],f=0);
);
);
f;
}
digits(n) = \\ The vector of the digits of n
{
return(eval(Vec(Str(n))))
}
droot(n) = \\ the digital root of a number.
{
local(x);
x= n%9;
if(x>0,return(x),return(9))
}
exponent(n) = \\ Return the exponent if n is a power
{
local(x,ln,j,e=0);
ln=omega(n);
x=factor(n);
e=x[1,2];
for(j=2,ln,
if(x[j,2] < e,e=x[j,2])
);
return(e)
}
find(str,match) = \\ Return the position of the first occurrence of string match in string str
{
local(lnm,lns,tstr,vstr,x,j);
vstr=Vec(Str(str));
match=Str(match);
lns=length(str);
lnm=length(match);
for(x=1,lns-lnm+1,
tstr="";
for(j=x,x+lnm-1,
tstr=concat(tstr,vstr[j]);
);
if(match==tstr,return(x))
);
return(0);
}
getcodes() = \\ Load Ascii character map for base conversion
{
local(j,ct); \\ vec ->ascii char
code = vector(258); \\ code is global to be called by base(r1,r2,num)
ct=0;
for(j=48,57, \\ 01-10->0123456789
ct++;
code[ct] = Strchr(j);
);
for(j=97,122, \\ 11-36->abcdefghijklmnopqrstuvwxyz good to base 36
ct++; \\ Lower case first to be compatible with Lcc
code[ct] = Strchr(j);
);
for(j=65,90, \\ 37-62->ABCDEFGHIJKLMNOPQRSTUVWXYZ good to base 62
ct++;
code[ct] = Strchr(j);
);
for(j=32,47, \\ 63-76-> !"#$%&'()*+,-./
ct++;
code[ct] = Strchr(j);
);
for(j=58,64, \\ 77-83->:;<=>?@
ct++;
code[ct] = Strchr(j);
);
for(j=91,96, \\ 84-90->:;<=>?@
ct++;
code[ct] = Strchr(j);
);
for(j=123,255, \\ 91-224->special ascii char
ct++;
code[ct] = Strchr(j);
);
for(j=1,31, \\ 225-255->special ascii char
ct++;
code[ct] = Strchr(j);
);
}
getcodes() \\ This globally loads the ascii tree suited for numbers compression
ifactor(n) = \\ The vector of the integer factors of n with multiplicity.
{
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)
}
ifactord(n) = \\ The vector of the distinct integer factors of n (without multiplicity).
{
local(f,j,k,flist);
flist=[];
f=Vec(factor(n));
for(j=1,length(f[1]),
flist = concat(flist,f[1][j])
);
return(flist)
}
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))
)
}
iscube(n) =
{
local(r);
r = n^(1/3);
if(floor(r+.5)^3== n,1,0)
}
isfourth(n) = \\ Return 1 if n is a fourt power
{
local(r);
r = sqrt(sqrt(n));
if(floor(r+.5)^4== n,1,0)
}
isinteger(n) =
{
if(n==floor(n),return(1),return(0))
}
issamedr(n) = \\ Return 1 if the factors of n with multiplicity have the same digital root else 0.
{
local(f,a,ln,x);
f=0;
a=ifactor(n);
ln=length(a);
for(x=1,ln-1,
if(droot(a[x])<>droot(a[x+1]),
f=1;break));
if(f==0&ln>1,return(1),return(0))
}