Sz\303\241m\303\255t\303\263g\303\251pes sz\303\241melm\303\251letJ\303\241rai AntalEzek a programok csak szeml\303\251ltet\303\251sre szolg\303\241lnak1. A pr\303\255mek eloszl\303\241sa, szit\303\241l\303\241s2. Egyszer\305\261 faktoriz\303\241l\303\241si m\303\263dszerek3. Egyszer\305\261 pr\303\255mtesztel\303\251si m\303\263dszerekLUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEhRicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0Yn4. Lucas-sorozatokrestart; with(numtheory);4.1. Defin\303\255ci\303\263.a:=-3; b:=2; for n to 10 do [(a^n-b^n)/(a-b),a^n+b^n]; od;4.2. Megjegyz\303\251s.4.3. P\303\251lda.a:=(1+sqrt(5))/2; b:=(1-sqrt(5))/2; for n to 10 do [expand((a^n-b^n)/(a-b)),expand(a^n+b^n)]; od;4.4. Rekurzi\303\263 sz\303\241mol\303\241shoz.#
# This procedure calculate the Lucas sequence terms
# [U[n],V[n],U[n+1],V[n+1]]. If the parameter N also given
# then the calculations are done mod N. Here U[n]=a^n-b^n)/(a-b)
# and V[n]=a^n+b^n, where a,b are the roots of x^2-Px+Q=0.
#
lucasUV:=proc(n,P,Q,N) local L,B,i,Qk;
B:=convert(n,base,2);
Qk:=1;
if nargs=3 then
L:=[0,2,1,P];
for i from nops(B) to 1 by -1 do
if B[i]=0 then
L:=[L[1]*L[2],L[2]^2-2*Qk,L[3]*L[2]-Qk,L[4]*L[2]-P*Qk];
Qk:=Qk^2;
else
L:=[L[3]*L[2]-Qk,L[4]*L[2]-P*Qk,L[3]*L[4],L[4]^2-2*Q*Qk];
Qk:=Qk^2*Q;
fi;
od;
else
L:=[0,2 mod N,1,P mod N];
for i from nops(B) to 1 by -1 do
if B[i]=0 then
L:=[L[1]*L[2] mod N,L[2]&^2-2*Qk mod N,\
L[3]*L[2]-Qk mod N,L[4]*L[2]-P*Qk mod N];
Qk:=Qk&^2 mod N;
else
L:=[L[3]*L[2]-Qk mod N,L[4]*L[2]-P*Qk mod N,\
L[3]*L[4] mod N,L[4]&^2-2*Q*Qk mod N];
Qk:=Qk&^2*Q mod N;
fi;
od;
fi; L end;#
# This procedure calculate the Lucas sequence terms
# [V[n],V[n+1]]. If the parameter N also given
# then the calculations are done mod N. Here V[n]=a^n+b^n,
# where a,b are the roots of x^2-Px+Q=0.
#
lucasV:=proc(n,P,Q,N) local L,B,i,Qk;
B:=convert(n,base,2);
Qk:=1;
if nargs=3 then L:=[2,P];
for i from nops(B) to 1 by -1 do
if B[i]=0 then
L:=[L[1]^2-2*Qk,L[2]*L[1]-P*Qk]; Qk:=Qk^2;
else
L:=[L[2]*L[1]-P*Qk,L[2]^2-2*Q*Qk]; Qk:=Qk^2*Q;
fi;
od;
else L:=[2 mod N,P mod N];
for i from nops(B) to 1 by -1 do
if B[i]=0 then
L:=[L[1]&^2-2*Qk mod N,L[2]*L[1]-P*Qk mod N]; Qk:=Qk&^2 mod N;
else
L:=[L[2]*L[1]-P*Qk mod N,L[2]&^2-2*Q*Qk mod N]; Qk:=Qk&^2*Q mod N;
fi;
od;
fi; L end;4.5. Feladat.4.6. Megjegyz\303\251s.4.7. Defin\303\255ci\303\263.4.8. T\303\251tel.4.9. Feladat.#
# This procedure do primality test for the odd number n
# using the set S of all factors of n+1 and the Lucas
# sequence corresponding to the roots of x^2-Px+1.
#
naiveplustest:=proc(n::posint,S::list(posint),P::integer)
local D,q,L;
D:=P^2-4; modp(D,4); if %=2 or %=3 then RETURN(FAIL) fi;
igcd(2*D,n);
if %<>1 then
if %<n then RETURN(false) else RETURN(FAIL) fi;
fi;
if jacobi(D,n)<>-1 then RETURN(FAIL) fi;
L:=lucasUV(n+1,P,1,n);
if L[1]<>0 or L[2]<>2 then RETURN(false) fi;
for q in S do
L:=lucasUV((n+1)/q,P,1,n);
if L[1]=0 and L[2]=2 then RETURN(FAIL) fi;
od; true end;naiveplustest(7,[2],3);#
# This procedure do primality test for the odd number n
# using the set S of all factors of n+1. Try all Lucas
# sequences with Q=1 and P in interval II.
#
naiveplustests:=proc(n::posint,S::list(posint),II::range(integer))
local r,P;
for P from op(1,II) to op(2,II) do
r:=naiveplustest(n,S,P);
if r<>FAIL then RETURN(r) fi;
od; FAIL end;naiveplustests(7,[2],1..2); naiveplustests(7,[2],1..3);4.10. Lucas-t\303\255pus\303\272 teszt.4.11. Feladat.#
# This procedure do primality test for the odd number n
# using a large enough set S of factors of n+1 and the Lucas
# sequence corresponding to the roots of x^2-Px+1.
#
plustest:=proc(n::posint,S::list(posint),P::integer)
local D,q,L; if n=1 then RETURN(false) fi;
D:=P^2-4; modp(D,4); if %=2 or %=3 then RETURN(FAIL) fi;
igcd(2*D,n);
if %<>1 then
if %<n then RETURN(false) else RETURN(FAIL) fi;
fi;
if jacobi(D,n)<>-1 then RETURN(FAIL) fi;
L:=lucasUV(n+1,P,1,n);
if L[1]<>0 or L[2]<>2 then RETURN(false) fi;
for q in S do
L:=lucasUV((n+1)/q,P,1,n);
igcd(L[2]-2,L[1],n);
if %<>1 then
if %<n then RETURN(false) else RETURN(FAIL) fi;
fi;
od; true end;plustest(7,[2],3);#
# This procedure do primality test for the odd number n
# using a large enough set S of factors of n+1. Try all Lucas
# sequences with Q=1 and P in interval II.
#
plustests:=proc(n::posint,S::list(posint),II::range(integer))
local r,P;
for P from op(1,II) to op(2,II) do
r:=plustest(n,S,P);
if r<>FAIL then RETURN(r) fi;
od; FAIL end;plustests(7,[2],1..2); plustests(7,[2],1..3);4.12. Feladat.#
# This procedure do primality test for the number h*2^n-1
# with n>1 and odd h<2^n using the Lucas sequence corresponding
# to the roots of x^2-Px+1.
#
specplustest:=proc(h::posint,n::posint,P::integer) local D,L,N;
if h>=2^n then error "first parameter is too large",h fi;
if type(h,even) then error "first parameter have to be odd",h fi;
if n<2 then error "second parameter is too small",h fi;
N:=h*2^n-1;
D:=P^2-4; modp(D,4); if %=2 or %=3 then RETURN(FAIL) fi;
igcd(2*D,N);
if %<>1 then
if %<n then RETURN(false) else RETURN(FAIL) fi;
fi;
if jacobi(D,N)<>-1 then RETURN(FAIL) fi;
L:=lucasUV(N+1,P,1,N);
if L[1]<>0 or L[2]<>2 then RETURN(false) fi;
L:=lucasUV((N+1)/2,P,1,N);
if L[1]<>0 then RETURN(false) fi;
if L[2]<>N-2 then
if L[2]=2 then RETURN(FAIL) else RETURN(false) fi;
fi; true end;specplustest(1,3,3);#
# This procedure do primality test for the number h*2^n-1
# with n>1 and odd h<2^n. Try all Lucas sequences with Q=1
# and P in interval II.
#
specplustests:=proc(h::posint,n::posint,II::range(integer))
local r,P;
if h>=2^n then error "first parameter is too large",h fi;
if type(h,even) then error "first parameter have to be odd",h fi;
if n<2 then error "second parameter is too small",h fi;
for P from op(1,II) to op(2,II) do
r:=specplustest(h,n,P);
if r<>FAIL then RETURN(r) fi;
od; FAIL end;specplustests(1,3,1..2); specplustests(1,3,1..3);4.13. Sz\303\241mtestek.4.14. T\303\251tel.4.15. Riesel-teszt.4.16. K\303\266vetkezm\303\251ny.4.17. Feladat.#
# This procedure calculate a^h+a^(-h) for a unit a=r+s*sqrt(D)
# with norm 1. Here r,s rational numbers, D must be a square
# free integer. All computations are done mod N.
#
rieselsetup:=proc(r,s,D,h,N) local P,a;
a:=r+s*sqrt(D);
P:=r+s*sqrt(D)+(r-s*sqrt(D))/(r^2-s^2*D);
if not type(P,integer) then ERROR(a+1/a, `not an integer`) fi;
lucasV(h,P,1,N)[1]; end;4.18. Feladat.#
# A procedure above for primality testing of h*2^n-1
# with n>1 and odd h<2^n for which h not divisible by 3.
#
rieselsqrt3:=proc(h::posint,n::posint) local i,v,N;
if h>=2^n then error "first parameter is too large",h fi;
if type(h,even) then error "first parameter have to be odd",h fi;
if modp(h,3)=0 then error "first parameter is a multiple of 3",h fi;
if n<2 then error "second parameter is too small",h fi;
N:=h*2^n-1; if modp(N,3)=0 then return false fi;
v:=rieselsetup(2,1,3,h,N);
for i to n-2 do v:=v^2-2 mod N od;
evalb(v=0); end;#
# Some tests.
#
test:=proc() local h,n;
for h in {1,5,7} do
for n from 4 to 20 do
print(isprime(h*2^n-1),rieselsqrt3(h,n))
od
od end;test();4.19. Feladat.#
# This procedure use iteration v<-v^2-2 starting with v=a^h+a^-h
# where the quadratic unit a=r+s*sqrt(D) is given for the
# primality testing of h*2^n-1. Here n>1 and h<2^n is odd.
#
riesel:=proc(h,n,r,s,D) local N,v,i;
if h>=2^n then error "first parameter is too large",h fi;
if type(h,even) then error "first parameter have to be odd",h fi;
if n<2 then error "second parameter is too small",h fi;
N:=h*2^n-1;
v:=rieselsetup(r,s,D,h,N);
for i to n-2 do v:=v^2-2 mod N od;
evalb(v=0); end;#
# Some tests.
#
test:=proc() local h,n;
for h in {1,5,7} do
for n from 4 to 20 do
print(isprime(h*2^n-1),riesel(h,n,2,1,3))
od
od end;test();4.20. Feladat.#
# This procedure find units having the form (k+l*sqrt(D))^2/r
# with small integers k,l and r=k^2-l^2*D in the
# quadratic extension of the rationals with sqrt(D).
# D must be a square free integer. The numbers with |k|,|l|<=B
# are tested. We remark that taking -r instead of r we get an
# other unit. The list of all [k,l,r] is given back.
#
findunit:=proc(D,B) local k,l,r,a,b,n,L;
L:=[];
for k from 0 to B do
for l from -B to B do
r:=k^2-l^2*D;
if r<>0 then
a:=(k^2+l^2*D)/r; b:=2*k*l/r;
if D mod 4=1 then n:=a^2-a*b-b^2*(D-1)/4
else n:=a^2-D*b^2 fi;
if n=1 and igcd(k,l,r)=1 then L:=[op(L),[k,l,r]] fi
fi
od
od; L end;for d in {2,3,5} do print(findunit(d,5)) od;4.21. Lucas-Lehmer-teszt a Mersenne-sz\303\241mokra.#
# This procedure do Lucas-Lehmer-test for Mersenne
# numbers M[n]=2^n-1. The exponents are taken from
# the list L. The result is the sequence of the
# for which M[n] is prime.
#
lucaslehmer:=proc(L::list(posint)) local n,M,i,vi,nL;
nL:=[];
for n in L do vi:=4; M:=2^n-1;
for i to n-2 do vi:=modp(vi&^2-2,M) od;
if vi=0 then nL:=[op(nL),n] fi;
od; nL end;Mersenne:=[2,3,5,7,13,17,19,31,67,127,257]; lucaslehmer(Mersenne);
L:=[i$i=2..257]; lucaslehmer(L);4.22. Feladat.4.23. Val\303\263sz\303\255n\305\261s\303\251gi teszt.4.24. Feladat.interface(verboseproc=2);print(`isprime`);4.25. Megjegyz\303\251s.4.26. Williams p+1 m\303\263dszere.4.27. Lemma.4.28. Feladat.#
# This procedure is Williams' p+1 method for factorization of n.
# P is the parameter for the Lucas sequence. Calculation of V's
# goes up to V_k!. The result is the factor found;
# gcd is calculated after m steps.
#
williams:=proc(n::posint,P::integer,k::posint,m::posint)
local g,x,i,j; x:=P;
for i from 2 by m to k do
for j from i to i+m-1 while j<=k do
x:=lucasV(j,x,1,n)[1];
od;
g:=igcd(x-2,n);
if g>1 then RETURN(g) fi;
od; igcd(x-2,n) end;williams(25852,3,10,1); williams(999863*999883*999907,3,1000,1);5. Alkalmaz\303\241sok 6. Sz\303\241mok \303\251s polinomok7. Gyors Fourier-transzform\303\241ci\303\2638. Elliptikus f\303\274ggv\303\251nyek9. Sz\303\241mol\303\241s elliptikus g\303\266rb\303\251ken10. Faktoriz\303\241l\303\241s elliptikus g\303\266rb\303\251kkel11. Pr\303\255mteszt elliptikus g\303\266rb\303\251kkel12. Polinomfaktoriz\303\241l\303\241s13. A szita m\303\263dszerek alapjai14. Az AKS tesztLUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEhRicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0Yn