Cod sursa(job #1040849)

Utilizator Mihai_ChihaiMihai Chihai Mihai_Chihai Data 24 noiembrie 2013 23:58:41
Problema Factorial Scor 25
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.78 kb
program factorial;
uses crt;
var n,p,min,max,mid,val:longint;
  u:boolean;

function fact(n:longint):longint;
var k,i:longint;
begin
k:=0;
i:=5;
while (n div i<>0) do begin
                  k:=k+n div i;
                  i:=i*5;
                  end;
fact:=k;
end;

begin
clrscr;
assign(input,'fact.in') ; reset(input);
readln(p) ; close(input);
assign(output,'fact.out') ; rewrite(output);
min:=1;
max:=10000000;
while (min<=max) and (not u) do begin
  mid :=(min+max) div 2;
  val:=fact(mid);
  if val<p then min:=mid+1
     else if val>p then max:=mid-1
       else u:=true
     end;
  if u then begin
        while mid mod 5<>0 do dec(mid);
        write(mid);
        end
        else write(-1);
  close(output);
end.















end.