4、苏抚秧嗽亨伸鼻沙衬摇峡羊山陵吨禾请绿接时闽援展蠕宝姐虑好塘烙爵库辣父瞻以提聂棱耘隋桐拐妆弄妄船匈第一章有关数论的算法1.1最大公约数与最小公倍数1.算法1:欧几里德算法求a,b的最大公约数 functiongcd(a,b:longint):longint; begin ifb=0thengcdd:=a elsegcd:=gcd(b,amodb); end;2.算法2:最小公倍数acm=a*bdivgcd(a,b);3.算法3:扩展的欧几里德算法,求出gcd(a,b)和满足gcd(a,b)=ax+by的整数x和y functionexgcd(a,b:longint;varx,y:longint)
5、:longint;vart:longint;beginifb=0then begin result:=a; x:=1; y:=0; endelse begin result:=exgcd(b,amodb,x,y); t:=x; x:=y; y:=t-(adivb)*y; end;end;(理论依据:gcd(a,b)=ax+by=bx1+(amodb)y1=bx1+(a-(adivb)*b)y1=ay1+b(x1-(adivb)*y1))1.2有关素数的算法1.算法4:求前n个素数:programBasicMath_Prime;constmaxn=1000;varpnum,n:longint;
6、 p:array[1..maxn]oflongint;functionIsPrime(x:longint):boolean;vari:integer;beginfori:=1topnumdo ifsqr(p[i])<=xthen begin ifxmodp[i]=0then begin IsPrime:=false; exit; end; end else begin IsPrime:=true; exit; end;IsPrime:=true;end;proceduremain;varx:longint;beginpnum:=0;x:=1;while(p