- 注册时间
- 2008-2-6
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 51573
- 在线时间
- 小时
|
楼主 |
发表于 2008-3-21 09:24:30
|
显示全部楼层
可爱的程序终于在CSDN搜索到了 :)
- program p27;
- //发现在10^14以内的极大素数间隔
-
- type
- primearray = array[1..664800] of longword;
- primesieve = array[1..4 * 3 * 5 * 7 * 11 * 13 * 17 * 19] of byte;
-
- const
- c01: double = 0.5;
-
- var
- p: primesieve;
- t: primesieve;
- sp: primearray;
- lp, rp: int64;
- start, stop: int64;
- prev: int64;
- pf: TextFile;
- s, csp: integer;
- cpuA, cpuB: int64;
-
- procedure compsieve;
- var
- i, j: integer;
- pp: array[1..8] of integer;
- begin
- pp[1] := 2;
- pp[2] := 3;
- pp[3] := 5;
- pp[4] := 7;
- pp[5] := 11;
- pp[6] := 13;
- pp[7] := 17;
- pp[8] := 19;
- for i := 1 to 4 * 3 * 5 * 7 * 11 * 13 * 17 * 19 do
- begin
- t[ i ] := 1;
- for j := 1 to 8 do
- if i mod pp[j] = 0 then
- begin
- t[ i ] := 0;
- end;
- end;
- end;
-
- procedure comptemp;
- var
- tf: file of primesieve;
- begin
- assignfile(tf, 'psieve.dat');
- {\$I-}
- reset(tf);
- read(tf, t);
- close(tf);
- {\$I+}
- if IOResult <> 0 then
- begin
- compsieve;
- rewrite(tf);
- write(tf, t);
- closefile(tf);
- end;
- end;
-
- procedure loadtemp;
- begin
- move(t, p, sizeof(p));
- end;
-
- function IsPrime(TestPrime: int64): boolean;
- var
- i: integer;
- k: int64;
- begin
- i := 1;
- k := round(sqrt(TestPrime * 1.0) + 0.5);
- while sp[ i ] <= k do
- begin
- if TestPrime mod sp[ i ] = 0 then
- begin
- IsPrime := False;
- exit;
- end;
- inc(i);
- end;
- IsPrime := True;
- end;
-
- procedure getcputime(var cputime: int64);
- begin
- asm
- push ebx
- push eax
- db \$0f, \$31
- mov ebx, eax
- pop eax
- mov dword ptr [eax], ebx
- mov dword ptr [eax + 4], edx
- pop ebx
- end;
- end;
-
- procedure findsmallprime;
- var
- i, j, k: longword;
- begin
- sp[1] := 3;
- sp[2] := 5;
- sp[3] := 7;
- sp[4] := 11;
- csp := 4;
-
- k := 13;
- while (k <= 9999999) do
- begin
- j := 1;
- i := round(sqrt(k) + 0.5);
- while sp[j] <= i do
- begin
- if k mod sp[j] = 0 then break;
- inc(j);
- end;
- if k mod sp[j] <> 0 then
- begin
- inc(csp);
- sp[csp] := k;
- end;
- k := k + 2;
- end;
- end;
-
- procedure loadprime;
- var
- p: file of primearray;
- begin
- assignfile(p, 'prime.dat');
- {\$I-}
- reset(p);
- read(p, sp);
- {\$I+}
- if IOResult <> 0 then
- begin
- FindSmallPrime;
- rewrite(p);
- write(p, sp);
- end;
- csp := 664578;
- closefile(p);
- end;
-
- procedure sieve;
- var
- j, k, n: int64;
- i, l: integer;
- m: longword;
- tmp1: longword;
- tmp2: longword;
- begin
- stop := start + 4 * 3 * 5 * 7 * 11 * 13 * 17 * 19 - 1;
-
- // getcputime(cpuA);
-
- loadtemp;
-
- //m := round(sqrt(stop * 1.0)+0.5)
- asm
- fild qword ptr [stop]
- fsqrt
- fadd qword ptr [c01]
- fistp dword ptr [m]
- end;
-
- i := 8;
-
- repeat
- tmp2 := sp[ i ];
- tmp1 := tmp2 * 2;
- n := tmp2 * tmp2;
- if n < start then j := start else j := n;
-
- // k := (j div tmp2) * tmp2;
- asm
- push ebx
- push ecx
- mov edx, 0
- mov eax, dword ptr [j + 4]
- mov ecx, tmp2
- div ecx
- mov ebx, eax
- mov eax, dword ptr [j]
- div ecx
- mul ecx
- mov dword ptr [k], eax
- mov eax, ebx
- mov ebx, edx
- mul ecx
- add eax, ebx
- mov dword ptr [k + 4], eax
- pop ecx
- pop ebx
- end;
-
- if k < start then k := k + tmp2;
- if k mod 2 = 0 then k := k + tmp2;
- while (k <= stop) do
- begin
- if p[k- start + 1] <> 0 then
- p[k - start + 1] := 0;
- k := k + tmp1;
- end;
- inc(i);
- until (sp[ i ] > m) or (i > csp);
-
- // getcputime(cpuB);
-
- // writeln(' Sieve cpu code time:', cpuB - cpuA);
-
- l := prev - start + 1;
-
- for i := 1 to 4 * 3 * 5 * 7 * 11 * 13 * 17 * 19 do
- begin
- if p[ i ] = 1 then
- begin
- if i - l >= s then
- begin
- lp := start + l - 1; rp := start + i - 1; s := i - l;
- writeln;
- writeln(lp:14, rp:14, s:14);
- writeln(pf, lp:14, rp:14, s:14);
- flush(pf);
- end;
- l := i;
- end;
- end;
- prev := start + l - 1;
- start := stop + 1;
- end;
-
- var
- bf: TextFile;
- bk: longword;
- begin
- loadprime;
- // writeln('加载素数表成功!');
- comptemp;
- // writeln('准备模版成功!');
- bk := 1;
- assignfile(bf, 'prime.ini');
- {\$I-}
- reset(bf);
- readln(bf, Start);
- if start mod 19399380 <> 1 then
- start := (start div 19399380) * 19399380 + 1;
- readln(bf, s);
- closefile(bf);
- {\$I+}
- if IOResult <> 0 then
- begin
- start := int64(17071454401);
- s := 382;
- end;
- Prev := Start;
- while not IsPrime(Prev) do
- Prev := Prev - 2;
- // writeln('加载开始点成功,start = ', start:14, ', prev = ', prev:14);
- assignfile(pf, 'p2.txt');
- append(pf);
- repeat
- sieve;
- // write('.');
- // writeln('现在进行下一个循环! prev = ', prev);
- inc(bk);
- if bk >= 100 then
- begin
- bk := 1;
- rewrite(bf);
- writeln(bf, start);
- writeln(bf, s);
- closefile(bf);
- end;
- until prev >= int64(100000000000000);
- closefile(pf);
- end.
复制代码 |
|