Solution (free pascal):
program Perfect;
var
N:longint;
fi,fo:text;
(*Check number perfect*)
function IsPerfect(num:longint):boolean;
var
i:longint;
sum:longint;
num_:longint;
begin
sum:=1;
num_:=num;
i:=2;
while(i*i<=num) do
begin
if((num mod i)=0) then
begin
sum :=sum+ i;
sum :=sum+(num div i);
end;
i :=i+ 1;
end;
if sum = num_ then
IsPerfect:=true
else
IsPerfect:=false;
(*
writeln('num=',num_,'sum=',sum);
readln;
*)
end;
begin
assign(fi,'Perfect.INP');reset(fi);
assign(fo,'Perfect.OUT');rewrite(fo);
read(fi,N);
(*write(N);*)
while N>1 do
begin
if(IsPerfect(N)) then
writeln(fo,N);
N:=N-1;
end;
(*readln; *)
close(fi);
close(fo);
end.
Không có nhận xét nào:
Đăng nhận xét