Показать сообщение отдельно
  #3  
Старый 09.08.2018, 06:27
lmikle lmikle вне форума
Модератор
 
Регистрация: 17.04.2008
Сообщения: 8,015
Версия Delphi: 7, XE3, 10.2
Репутация: 49089
По умолчанию

Вот, поправил:
Код:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  int_Fibs : Array Of Int64;

function Fib(N : Integer) : Int64;
var
  I : Integer;
begin
  If N < 1 Then Result := -1 Else
    If N <= Length(int_Fibs) Then Result := int_Fibs[N-1] Else
      Begin
        For I := Length(int_Fibs) To N-1 Do
          Begin
            SetLength(int_Fibs,Length(int_Fibs)+1);
            int_Fibs[i] := int_Fibs[I-1] + int_Fibs[I-2];
          End;
        Result := int_Fibs[High(int_Fibs)];
      End;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I : Integer;
begin
  Memo1.Lines.Clear;
  For I := 1 To 150 Do
    Memo1.Lines.Add(Format('Fib(%d) = %d',[I,Fib(I)]));
end;

initialization
  SetLength(int_Fibs,2);
  int_Fibs[0] := 1;
  int_Fibs[1] := 1;

finalization
  SetLength(int_Fibs,0);

end.

Но там все-равно 150 не дает, где-то уже с 90 начинаются проблемы. Даже int64 не хватает. Из идей - делать 2 int64 и "пилить" числа. Либо искать библиотеку для BigInteger (типа для 128 битных чисел или больше). Ну или делать через строку и самому писать алгоритм скложения.
Ответить с цитированием