Sign in Join
Wiki copytemporary

Fibonacci number/ru

Deutsch · English · Français · Suomi · Русский

Числа Fibonacci

Числа Fibonacci задаются следующей последовательностью:

text
0, 1, 1, 2, 3, 5, 8, 13, 21, ...

Идея заключается в сложении двух последних чисел, и этот результат является следующим значением последовательности.

Рекурсивный способ

pascal

function FibonacciNumber( n : integer ): integer;
begin
  if n > 1 then result := ( FibonacciNumber( n - 1 ) + FibonacciNumber( n - 2 ) )
    else
      if n = 0 then result := 0
        else result := 1;
end;

Итеративный способ

Вот один из предпочтительных вариантов.

pascal
function Fibonacci(n: Integer): Integer;
var
  i,u,v,w: Integer;
begin
  if n <= 0 then
    exit(0);
  if n = 1 then 
     exit(1);
  u := 0;
  v := 1;
  for i := 2 to n do 
  begin
    w := u + v;
    u := v;
    v := w;
  end;
  Result := v;
End;

См. также


From the Free Pascal wiki, page "Fibonacci number/ru", as it stood on 2020-02-14 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .

Edited on wiki.freepascal.org by 2 people, 2018–2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
UseroflazarusCreated page
TrevFixed syntax highlighting; deleted category included in page template (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.