问题2:因为是环形,可以令第一个元素为1
函数如下:
function GetSequence:Integer;
const
N=10;
PrimeTable: Array [0..4*N-1] of Boolean=(
// 0 1 2 3 4 5 6 7 8 9
False, False, True , True , False, True , False, True , False, False,
False, True , False, True , False, False, False, True , False, True ,
False, False, False, True , False, False, False, False, False, True ,
False, True , False, False, False, False, False, True , False, False);
OrdTable: Array [0..N-1] of Byte=(1,3,5,7,9,11,13,15,17,19);
EvenTable: Array [0..N-1] of Byte=(2,4,6,8,10,12,14,16,18,20);
var
Ords,Evens: Array [0..N-1] of Byte;
FreeOrd,FreeEven: Array [0..N-1] of Boolean;
procedure FetchEven(Start:Byte);
forward;
procedure FetchOdd(Start:Byte);
var i: Byte;
begin
for i:=1 to N-1do
if FreeOrd then
begin
FreeOrd:=False;
if PrimeTable[Evens[Start-1]+OrdTable ] then
begin
Ords[Start]:=OrdTable;
FetchEven(Start);
end;
FreeOrd:=True;
end;
end;
procedure FetchEven(Start:Byte);
forward;
var i:Byte;
begin
for i:=0 to N-1do
if FreeEven then
begin
FreeEven:=False;
if PrimeTable[EvenTable+Ords[Start] ] then
if Start=N-1 then
begin
Events[Start]:=EvenTable;
if PrimeTable[Events[Start]+1]
then
Inc(Result);
//将该语句替换成输出语句就可以看到结果
end else
begin
Events[Start]:=EvenTable;
FetchOrd(Start+1);
end;
FreeEven:=True;
end;
end;
begin
Result:=0;
FillChar(FreeOrd,N,1);
FillChar(FreeEven,N,1);
Ords[0]:=1;
FreeOrd[0]:=False;
FetchEven[0];
end;