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\263dszerekrestart; with(numtheory);2.1. Pr\303\263baoszt\303\241s.#
# This is a simple factorization
# procedure using trial division.
# The result is a sequence of pairs
# [p,e] where the p's are the prime
# factors and the e's are the exponents.
# The factors are anyway in increasing order.
# Only primes <= P are tried, hence the
# last "factor" may composite, if
# it is greater then P^2;
#
trialdiv:=proc(n::posint,P::posint) local L,p,i,d,nn;
L:=[]; nn:=n;
if type(nn,even) and 2<=P then
for i from 0 while type(nn,even) do nn:=nn/2; od;
L:=[[2,i]];
fi;
if nn mod 3=0 and 3<=P then
for i from 0 while nn mod 3=0 do nn:=nn/3; od;
L:=[op(L),[3,i]];
fi;
d:=2; p:=5;
while p<=P and nn>=p^2 do
if nn mod p=0 then
for i from 0 while nn mod p=0 do nn:=nn/p; od;
L:=[op(L),[p,i]];
fi;
p:=p+d; d:=6-d;
od;
if nn>1 then L:=[op(L),[nn,1]] fi;
L;
end;trialdiv(2^32+1,1000);#
# This is a simple primality testing
# procedure using trial division.
# The result is true if n is prime, else false.
#
trialprime:=proc(n::posint) local L,p,i,d,nn;
if n=1 then RETURN(false) fi;
if n=2 or n=3 or n=5 then RETURN(true) fi;
if type(n,even) then RETURN(false) fi;
if n mod 3=0 then RETURN(false) fi;
d:=2; p:=5;
while n>=p^2 do
if n mod p=0 then RETURN(false) fi;
p:=p+d; d:=6-d;
od; true; end;trialprime(2^32+1);2.2. Feladat.2.3. Feladat.interface(verboseproc=2);print(ifactor);print(`ifactor/ifact235`);print(`ifactor/ifact0th`);print(`ifactor/ifact1st`);print(`ifactor/wheelfact`);print(`ifactor/pollp1`);print(`ifactor/pp100000`);print(`ifactor/easy`);print(`ifactor/ifact235`);2.4. A pr\303\255moszt\303\263k eloszl\303\241s\303\241r\303\263l.2.5. A pr\303\255moszt\303\263k sz\303\241m\303\241nak hat\303\241reloszl\303\241sa.2.6. Fermat m\303\263dszere.#
# This procedure prepare the sieve table S for
# Fermat's factorization procedure. Parameter n is the
# integer to factor and m is the vector of moduli.
#
preparefermatsieve:=proc(n,S,m) local i,j,k,x2,x2n;
x2:=table; x2n:=table;
for i to nops(m) do
for j from 0 to m[i]-1 do S[i,j]:=0; od;
for j from 0 to m[i]-1 do
x2[j]:=j^2 mod m[i];
x2n[j]:=j^2-n mod m[i];
od;
for j from 0 to m[i]-1 do
for k from 0 to m[i]-1 do
if x2n[j]=x2[k] then S[i,j]:=1 fi;
od;
od;
od; end;#
# This procedure do factorization with
# Fermat's method. Parameter n is
# the odd number to factor and m is the list of moduli.
# Returns with u where u is the largest
# factor of n less then or equal to sqrt(n).
#
fermatfactorization:=proc(n::posint,m::list(posint))
local k,x,y,i,S,r,f;
if type(n,even) then error "first argument must be odd" fi;
S:=table(); preparefermatsieve(n,S,m); r:=nops(m);
k:=array(1..r); x:=isqrt(n);
for i to r do k[i]:=-x mod m[i]; od;
while true do
f:=true;
for i to r do if S[i,k[i]]<>1 then f:=false; break; fi; od;
if f then
y:=isqrt(x^2-n);
if y^2=x^2-n then RETURN(x-y) fi;
fi;
x:=x+1;
for i to r do k[i]:=k[i]-1 mod m[i]; od;
od; end;debug(fermatfactorization);
fermatfactorization(13*17,[3,5,7,8,11]);undebug(fermatfactorization);
fermatfactorization(11111,[3,5,7,8,11]);2.7. Feladat.2.8. Feladat.2.9. Pollard \317\261 m\303\263dszere.Az n sz\303\241m has\303\255t\303\241sa az x->x^2+c f\303\274ggv\303\251ny felhaszn\303\241l\303\241s\303\241val; g egy iter\303\241ci\303\263csoportm\303\251rete \303\251s legfeljebb maxgs csoport fog v\303\251grehajt\303\263dni.pollardrhosplit:=proc(n::posint,c::posint,g::posint,maxgs::posint)
local x,xx,xp,xo,xpo,i,j,k,ko,l,lo;
x:=1+c mod n; xp:=1; i:=0; k:=1; l:=1; xx:=1;
while igcd(xx,n)=1 and i<maxgs do
xo:=x; xpo:=xp; ko:=k; lo:=l; j:=0; xx:=1;
while j<g do
xx:=xx*(xp-x) mod n;
k:=k-1; if k=0 then xp:=x; k:=l; l:=2*l; fi;
x:=x^2+c mod n; j:=j+1;
od; i:=i+1;
od;
if igcd(xx,n)<n then return(igcd(xx,n)) fi;
x:=xo; xp:=xpo; k:=ko; l:=lo; j:=0;
while igcd(xp-x,n)=1 and j<g do
k:=k-1; if k=0 then xp:=x; k:=l; l:=2*l; fi;
x:=x^2+c mod n; j:=j+1;
od;
igcd(xp-x,n); end;pollardrhosplit(999863*999883,1,2^4,2^5);2.10. Feladat.2.11. Fermat t\303\251tele.2.12. Euler t\303\251tele.2.13. K\303\255nai marad\303\251kt\303\251tel.chrem([1,2,2],[2,3,7]);2.14. T\303\251tel.2.15. Gyors hatv\303\241nyoz\303\241s.#
# Calculation of modular power of a
# with the left-to-right binary method.
#
left2right:=proc(a,e::posint,mult::procedure) local b,x,n;
b:=convert(e,base,2); x:=a;
for n from nops(b)-1 by -1 to 1 do
x:=mult(x,x);
if b[n]>0 then x:=mult(x,a); fi;
od; x; end;debug(left2right); left2right(2,11,(x,y)->x*y);#
# Calculation of a modular power
# with the left-to-right 2^m-ary method.
#
fastexp:=proc(a,e::posint,m::posint,mult::procedure)
local i,j,k,P,x,b,aa,n;
b:=convert(e,base,2); n:=nops(b)-1; x:=a; P:=[a]; aa:=mult(a,a);
for j from 2 to 2^(m-1) do P:=[op(P),mult(P[nops(P)],aa)]; od;
while true do
if n=0 then return(x) fi;
if b[n]=0 then x:=mult(x,x); n:=n-1; next; fi;
i:=1; j:=1; k:=0; x:=mult(x,x); n:=n-1;
while n>0 and k+j<m do
if b[n]=0 then k:=k+1; n:=n-1;
else k:=k+1; j:=j+k; i:=i*2^k;
while k>0 do x:=mult(x,x); k:=k-1; od;
n:=n-1;
fi;
od;
x:=mult(x,P[i]);
while k>0 do x:=mult(x,x); k:=k-1; od;
od; end;debug(fastexp); fastexp(2,11,1,(x,y)->x*y);debug(fastexp); fastexp(2,11,2,(x,y)->x*y);2.16. Feladat.2.17. Pollard p-1 m\303\263dszere.#
# This procedure is Pollard's p-1 method for
# factorization. The base is a, and powers of
# primes up to P are considered so that they
# are not less then the bound B.
# The result is the power x of a mod n, where
# n isthe number to factorize, so the factor is gcd(x-1,n).
#
pollardpsplit:=proc(n,a,B,P) local e,d,p,x;
x:=a mod n;
if igcd(x-1,n)>1 or P<2 then return(x) fi;
if P<2 then return(x) fi;
e:=1; while 2^e<B do e:=e+1 od;
x:=x&^(2^e) mod n;
if igcd(x-1,n)>1 or P=2 then return(x) fi;
while 3^e>3*B and e>1 do e:=e-1 od;
x:=x&^(3^e) mod n;
d:=2; p:=5;
while true do
if igcd(x-1,n)>1 or P<p then return(x) fi;
while p^e>p*B and e>1 do e:=e-1 od;
x:=x&^(p^e) mod n;
p:=p+d; d:=6-d;
od; x; end;pollardpsplit(25852,2,100,100);igcd(%-1,25852);pollardpsplit(999863*999917*999961,23,2000,1000);igcd(%-1,999863*999917*999961);2.18. Feladat.2.19. Pollard p-1 m\303\263dszere, m\303\241sodik l\303\251pcs\305\221.#
# This procedure is the second step of Pollard's p-1 method for
# factorization. The base is a, and primes from list P
# are considered. The result is the power x of a mod n, where
# n is the number to factorize, so the factor is gcd(x-1,n).
#
pollardp2split:=proc(n::posint,a::posint,N::posint,m::posint,M::posint)
local x,i,j,E,aa,p,pp,d;
E:=Array(1..N); aa:=a*a mod n; E[1]:=aa;
for j from 2 to N do E[j]:=E[j-1]*aa od;
p:=ithprime(m); x:=a&^p mod n;
for i from m+1 to M while gcd(x-1,n)=1 do
pp:=nextprime(p); d:=pp-p; p:=pp;
if d<=2*N then x:=x*E[d/2] mod n;
else x:=x*(a&^d) mod n; fi;
od; x; end;pollardpsplit(8174912477117*23528569104401,3,1000,1000);igcd(%-1,8174912477117*23528569104401);pollardp2split(8174912477117*23528569104401,%%,100,100,10000);igcd(%-1,8174912477117*23528569104401);ifactor(%-1);2.20. Feladat.3. Egyszer\305\261 pr\303\255mtesztel\303\251si m\303\263dszerekLUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEhRicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0Yn4. Lucas-sorozatok5. 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. Az AKS teszt14. A szita m\303\263dszerek alapjaiLUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEhRicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0Yn