Sign in Join
Wiki copytemporary

Fibonacci number/ru: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Fibonacci number/ru

OldNewLine
1+# Числа Fibonacci
2+
3+Числа Fibonacci задаются следующей последовательностью:
4+
5+`0, 1, 1, 2, 3, 5, 8, 13, 21, ...`
6+
7+Идея заключается в сложении двух последних чисел, и этот результат является следующим значением последовательности.
8+
9+## Рекурсивный способ
10+
11+``` pascal
12+
13+function FibonacciNumber( n : integer ): integer;
14+begin
15+ if n > 1 then result := ( FibonacciNumber( n - 1 ) + FibonacciNumber( n - 2 ) )
16+ else
17+ if n = 0 then result := 0
18+ else result := 1;
19+end;
20+```
21+
22+## Итеративный способ
23+
24+Вот один из предпочтительных вариантов.
25+
26+``` pascal
27+function Fibonacci(n: Integer): Integer;
28+var
29+ i,u,v,w: Integer;
30+begin
31+ if n <= 0 then
32+ exit(0);
33+ if n = 1 then
34+ exit(1);
35+ u := 0;
36+ v := 1;
37+ for i := 2 to n do
38+ begin
39+ w := u + v;
40+ u := v;
41+ v := w;
42+ end;
43+ Result := v;
44+End;
45+```
46+
47+## См. также
48+
49+- [Some assembly routine which uses the C calling convention that calculates the nth Fibonacci number](http://www.freepascal.org/docs-html/prog/progsu151.html)
50+- [[Solution 3|Tao Yue Solution to Fibonacci Sequence Problem]]
51+
52+---
53+*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](/wiki-contact).*

Back to history