- 11
- 0
- 约2.04万字
- 约 57页
- 2018-04-03 发布于江西
- 举报
数论算法(Number Theory Algorithms).doc
数论算法(Number Theory Algorithms)
First, number theoretic algorithm
The greatest common divisor of 1.
Function GCD (a, b:integer): integer;
Begin
If b=0 then gcd:=a
Else gcd:=gcd (B, a mod B);
End;
The least common multiple of the 2.
Function LCM (a, b:integer): integer;
Begin
If ab then swap (a, B);
Lcm:=a;
While LCM mod b0 do Inc (LCM, a);
End;
The solution of 3. prime numbers
A. determine if a number is prime in a small range:
Function prime (n: integer): Boolean;
Var I: integer;
Begin
For I:=2 to TRUNC (sqrt (n)) do
If n mod I=0 then begin
Prime:=false; exit;
End;
Prime:=true;
End;
B. determines whether the number within the range of Longint is a prime number (including a prime table less than 50000):
Procedure getprime;
Var
I, j:longint;
P:array[1..50000] of boolean;
Begin
Fillchar (P, sizeof (P), true);
P[1]: =false;
I:=2;
While i50000 do begin
If p[i] then begin
J:=i*2;
While j50000 do begin
P[j]: =false;
Inc (J, I);
End;
End;
Inc (I);
End;
L:=0;
For i:=1 to 50000 do
If p[i] then begin
Inc (L); pr[l]: =i;
End;
End; {getprime}
Function prime (x:longint): integer;
Var i:integer;
Begin
Prime:=false;
For i:=1 to l do
If pr[i]=x then break
Else if x mod pr[i]=0 then exit;
Prime:=true;
End; {prime}
Two, graph theory algorithm
1. minimum spanning tree
A.Prim algorithm:
Procedure prim (v0:integer);
Var
Lowcost, closest:array[1..Maxn] of integer;
I, J, K, min:integer;
Begin
For i:=1 to n do begin
Lowcost[i]: =cost[v0, i];
Closest[i]: =v0;
End;
For i:=1 to n-1 do begin
{look for the nearest vertex k} not spanning the spanning tree
Min:=maxlongint;
For j:=1 to n do
If (lowcost[j]min) and (lowcost[j]0) then begin
Min:=lowcost[j];
K:=j;
End;
Lowcost[k]: =0; {vertex K is added to the spanning tree}
Add a new edge K to closest[k]} in the {spanning tree
{modify the lowcost and closest values of each point}
For j:=1 to n do
If cost[k, j]lwocost[j] then begin
Lowcost[j]: =cost[k, j];
Closest[j]: =k;
End;
End;
End; {prim}
B.Kruskal algorithm: (Tan Xin)
The edges of the graph are d
原创力文档

文档评论(0)