program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils,
Math,
Windows;
function GetCPUTick: Int64;
asm
DB $0F,$31
end;
function SeEntao(AValue: Boolean; const ATrue: String; const AFalse: String): String;
begin
if AValue then//Verifica se AValue é verdadeiro;
Result := ATrue//Se for retorna valor verdadeiro;
else
Result := AFalse;//Se não retorna valor falso;
end;
function Fib(I,Qtd: Real): String;
var
//Define variaveis do tipo flutuante
N1,N2,N3: Real;
//Define variavel do tipo inteiro
T1,T2,X,Y: Integer;
Tag: String;
begin
//Adiciona o Tempo inicial do algoritimo
T1:=GetCPUTick;
//Valor do Numero antecessor
N1:=1;
//Valor do Numero atual
N2:=1;
//Valor do Proximo numero
N3:=1;
//Contador do loop
X:=0;
//Enquanto X for menor que (I) Inicio de retorno mais (+) Quantidade de retorno (Qtd) não sai do loop
While X<I+Qtd do
begin
//Se X menor ou igual a (I) Inicio do retorno
if X>=I then
begin
//Define Resultado (Result) igual a Resultado (Result) mais (+) (Se resultado em branco retorna em branco, se não Retorna quebra de linha) mais (+) Numero da lista (N3);
Result:=Result+SeEntao(Result='','',#10#13)+FloatToStr(N3);
end;
//Define Proximo numero (N3) igual a Numero Antecessor (N1) mais (+) Numero atual (N2);
N3:=N1+N2;
//Define Numero antecessor (N1) igual a Numero atual (N2)
N1:=N2;
//Define Numero atual igual a Proximo numero
N2:=N3;
//Incluir mais (+) 1 a X;
Inc(X);
end;
//Pega tempo inicial do algoritimo
Y:=0;
T2:=GetCPUTick-T1;
Tag:='ns';
while T2 div 1000>0 do
begin
T2:=T2 div 1000;
Inc(Y);
end;
Case Y of
0:
Tag:='ns';
1:
Tag:='mcs';
2:
Tag:='ms';
end;
Result:=Result+#10#13+'Tempo decorrido no algoritimo e de: '+IntToStr(T2)+Tag;
end;
begin
try
//Chama função definindo valor inicial de retorno e quantidade a retornar
Writeln(Fib(0,-20));
except
//Se exceder o limite do ponto flutuante retorna o Erro
On E: Exception do
begin
Writeln(E.Message);
end;
end;
Readln;
end.