PYTHAEQ - Phương trình Pythagore nghiệm nguyên

Tác giả: flashmt

Ngôn ngữ: Pascal

const fi='';
      fo='';
      maxk=44721;
var r,re:longint;
    a:array[2..maxk] of byte;
    b,b1:array[0..5000] of longint;
    c:array[0..5000] of longint;

procedure rf;
begin
     assign(input,fi);
     reset(input);
     read(r);
     if r<0 then r:=-r;
     close(input);
end;

procedure prime;
var i,j,k:longint;
begin
     fillchar(a,sizeof(a),0);
     fillchar(b,sizeof(b),0);
     fillchar(b1,sizeof(b1),0);
     k:=trunc(sqrt(maxk));
     for i:=2 to k do
         if a[i]=0 then
         begin
              j:=i*i;
              while j<=maxk do
              begin
                   a[j]:=1;
                   j:=j+i;
              end;
         end;
     for i:=3 to maxk do
         if (a[i]=0) then
         begin
              if i mod 4 =1 then
              begin
                   inc(b[0]);
                   b[b[0]]:=i;
              end;
              if i mod 4 =3 then
              begin
                   inc(b1[0]);
                   b1[b1[0]]:=i;
              end;
         end;
end;

procedure init;
var i:longint;
begin
     while r mod 2 = 0 do r:=r div 2;
     for i:=1 to b1[0] do
         while r mod b1[i] = 0 do
               r:=r div b1[i];
end;

procedure pr;
var i,j:longint;
begin
     re:=0;
     if (r=0) then exit;
     prime;
     init;
     fillchar(c,sizeof(c),0);
     for i:=1 to b[0] do
     begin
          if r mod b[i] = 0 then
          begin
               inc(c[0]);
               c[c[0]]:=1;
               r:=r div b[i];
               while r mod b[i]=0 do
               begin
                    inc(c[c[0]]);
                    r:=r div b[i];
               end;
          end;
     end;
     if r<>1 then
     begin
          inc(c[0]);
          c[c[0]]:=1;
     end;
     j:=1;
     for i:=1 to c[0] do
         j:=j*(2*c[i]+1);
     j:=(j-1) div 2;
     re:=j;
end;

procedure wf;
begin
     assign(output,fo);
     rewrite(output);
     if r=0 then write(1) else write(re*8+4);
     close(output);
end;

begin
     rf;
     pr;
     wf;
end.

Download