Difference between revisions of "Fibonacci number/fr"
From Free Pascal wiki
Jump to navigationJump to searchm |
m (Fixed syntax highlighting) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 8: | Line 8: | ||
== Méthode récursive == | == Méthode récursive == | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
− | |||
function FibonacciNumber( n : integer ): integer; | function FibonacciNumber( n : integer ): integer; | ||
begin | begin | ||
Line 21: | Line 20: | ||
== Méthode itérative == | == Méthode itérative == | ||
+ | |||
Celle-ci est préférable. | Celle-ci est préférable. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
− | |||
function Fibonacci(n: Integer): Integer; | function Fibonacci(n: Integer): Integer; | ||
var | var | ||
Line 47: | Line 46: | ||
== Voir aussi == | == Voir aussi == | ||
− | * | + | * [https://fr.wikipedia.org/wiki/Suite_de_Fibonacci Suite de Fibonacci] |
+ | * [http://www.freepascal.org/docs-html/prog/progsu151.html Some assembly routine which uses the C calling convention that calculates the nth Fibonacci number] | ||
* [[Solution_3| Solution de Tao Yue au problème de la suite de Fibonacci]] | * [[Solution_3| Solution de Tao Yue au problème de la suite de Fibonacci]] |
Latest revision as of 00:12, 15 February 2020
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
Nombre de Fibonacci
La suite de Fibonacci est la suivante : 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
Chaque nombre est la somme de ses deux prédécesseurs, les deux premiers termes sont 0 et 1.
Méthode récursive
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;
Méthode itérative
Celle-ci est préférable.
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;