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

Tác giả: RR

Ngôn ngữ: Pascal

//Written by Nguyen Thanh Trung
{$R+,Q+}
{$Mode objFPC}
uses math;
const
  FINP='';
  FOUT='';
var
  n:longint;
  f1,f2:text;
procedure openF;
begin
  assign(f1,FINP); reset(f1);
  assign(f2,FOUT); rewrite(f2);
end;
procedure closeF;
begin
  close(f1); close(f2);
end;
function gcd(a,b:longint):longint; inline;
begin
  if (a=0) or (b=0) then exit(a+b)
  else if a<b then exit(gcd(b,a))
  else exit(gcd(b,a mod b))
end;
function count(u:longint):longint; inline;
var
  sum,i,j:longint;
begin
  sum:=0;
  for i:=1 to u-1 do
    begin
      j:=trunc(sqrt(u-i*i));
      if j<=i then break;
      if (j*j+i*i=u) and (gcd(i,j)=1) and (gcd(j*j-i*i,j*i*2)=1) then
        sum+=8;
    end;
  exit(sum);
end;
procedure solve;
var
  i,x,kq:longint;
begin
  kq:=4;
  for i:=2 to n do
  begin
    if n div i<i then break;
    if n mod i=0 then
      begin
        x:=n div i;
        if x<i then break;
        kq+=count(i);
        if x<>i then kq:=kq+count(x);
      end;
  end;
  kq+=count(n);
  writeln(f2,kq);
end;
begin
  openF;
  read(f1,n); if n<0 then n:=-n;
  solve;
  closeF;
end.

Download