const maxcoresize=64000;
var   core :array[0..maxcoresize-1] of boolean;
      vectora:array[1..20] of 0..maxcoresize;
      vectorb:array[1..20] of double;
      m    :word;
      coresize:word;
      code :integer;
      max  :word;

function gcd(a,b:word):word;           { greatest common divisor }
begin
while (a<>0) and (b<>0) do
      if a>b then a:=a mod b else b:=b mod a;
gcd:=a+b;
end;

procedure modulo;
var i,j,d:word;
    a:longint;
    b,c:word;
    dong:double;
    count:word;
begin
for i:= m to max do begin
    if gcd(coresize,i)=m then begin     { modulo m ? }
       a:=0;
       dong:=0;
       fillchar(core,sizeof(core),#0);   { no hit at start }
       core[0]:=true;                    { first hit }
       count:=coresize div m;            { how many hits to finish }
       for j:=2 to count do begin
           a:=(a+i) ;
           if a>=coresize then a:=a-coresize;
           core[a]:=true;                { next hit }
           b:=a;c:=a;
           d:=0;                  { this will contain the distance }
           repeat                 { find closest previous hit }
                 inc(d,m);
                 if b<m then b:=b+coresize;
                 b:=b-m ;   { search on the left }
                 c:=c+m;    { and on the right }
                 if c>=coresize then c:=c-coresize;
           until (core[b]=true) or (core[c]=true);   { found }
           dong:=dong+d ;          { we are going to take the average }
       end;
       dong:=dong/(count-1);
       if dong>vectorb[20] then begin    { keep track of the 20 best }
          vectora[20]:=i;
          vectorb[20]:=dong;
       for j:=20 downto 2 do
           if vectorb[j]>vectorb[j-1] then begin
              dong:=vectorb[j];vectorb[j]:=vectorb[j-1];vectorb[j-1]:=dong;
              a:=vectora[j];vectora[j]:=vectora[j-1];vectora[j-1]:=a;
           end;
      {     writeln(vectora[1],vectorb[1]);      }
       end;
    end;

end;
writeln('constant      value');    { print the result }
for i:=1 to 20 do
    writeln(vectora[i],vectorb[i]:20:5);
end;

begin
writeln(gcd(10,6));
writeln;
writeln('Program to calculate optima type constants for the game Corewar');
writeln('by Nandor Sieben  6/29/93');
if paramcount<2 then begin
   writeln('usage:');
   writeln('optima coresize modulo range');
   writeln('will find the best 20 contants that is less than range');
   writeln('choose modulo=1 for bombing every core location');
   exit;
end;
fillchar(vectora,sizeof(vectora),#0);
fillchar(vectorb,sizeof(vectorb),#0);
writeln;
val(paramstr(1),coresize,code);
val(paramstr(2),m,code);
if paramcount=2 then
   max:=coresize div 2
else
    val(paramstr(3),max,code);
writeln('working...');
modulo;
end.