Suite de Fibonacci

Description du code

Exemple de méoïsation en Pascal

Code source ou contenu du fichier

  1. program fibonacci-memo;
  2.  
  3. cons undef = -1; {valeur qui n'est jamais employee et qui signifie une valeur non definie}
  4. cons maxIndice = 20;
  5.  
  6. var memoArray : packed array[0..maxIndice] of integer;
  7.  
  8. function fibonacci(var n : integer) : integer;
  9. {Pre: 0 <= n <= maxIndice}
  10. begin
  11. if n < 2
  12. then
  13. fibonacci := n;
  14. else
  15. fibonacci := fibonacciMemo(n-1) + fibonacciMemo(n-2);
  16. end;
  17.  
  18. function fibonacciMemo(var n : integer) : integer;
  19. {Pre: 0 <= n <= maxIndice}
  20. begin
  21. if memoArray[n] = undef
  22. then
  23. memoArray[n] := fibonacci(n);{il est necessaire de calculer la valeur}
  24. fibonacciMemo := memoArray[n];{la valeur est deja calculee}
  25. end;
  26.  
  27. {main program}
  28. begin
  29. var i : integer;
  30. var j : integer;
  31. j:=20;
  32. for i:=0 to maxIndice do
  33. memoArray[i]:=undef;
  34. writeln('Fibonacci(', j, ') = ', fibonacci(j));
  35. end.

Autres extraits de codes en Pascal

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 05/10/2009, last modified the 28/10/2018
Source of the printed document:https://www.gaudry.be/en/sniplet-rf-pascal/fibonacci-memo.pas.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.