Sign in Join
Wiki copytemporary

Fibonacci number/ru: what changed

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

OldNewLine
11# Числа Fibonacci
22
33Числа Fibonacci задаются следующей последовательностью:
44
5-`0, 1, 1, 2, 3, 5, 8, 13, 21, ...`
5+```text
6+0, 1, 1, 2, 3, 5, 8, 13, 21, ...
7+```
68
79Идея заключается в сложении двух последних чисел, и этот результат является следующим значением последовательности.
810
911## Рекурсивный способ
1012
1113``` pascal
1214
1315function FibonacciNumber( n : integer ): integer;
1416begin
1517 if n > 1 then result := ( FibonacciNumber( n - 1 ) + FibonacciNumber( n - 2 ) )
1618 else
1719 if n = 0 then result := 0
1820 else result := 1;
1921end;
2022```
2123
2224## Итеративный способ
2325
2426Вот один из предпочтительных вариантов.
2527
2628``` pascal
2729function Fibonacci(n: Integer): Integer;
2830var
2931 i,u,v,w: Integer;
3032begin
3133 if n <= 0 then
3234 exit(0);
3335 if n = 1 then
3436 exit(1);
3537 u := 0;
3638 v := 1;
3739 for i := 2 to n do
3840 begin
3941 w := u + v;
4042 u := v;
4143 v := w;
4244 end;
4345 Result := v;
4446End;
4547```
4648
4749## См. также
4850
4951- [Some assembly routine which uses the C calling convention that calculates the nth Fibonacci number](http://www.freepascal.org/docs-html/prog/progsu151.html)
5052- [[Solution 3|Tao Yue Solution to Fibonacci Sequence Problem]]
5153
5254---
5355*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