Advertisement
viraco4a

FibonacciRecursionMemoization

May 28th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Fibonacci
  6. {
  7. class Program
  8. {
  9. static int[] numbers;
  10.  
  11. static int Fib(int n)
  12. {
  13. if (numbers[n] != 0)
  14. {
  15. return numbers[n];
  16. }
  17.  
  18. if (n == 1 || n == 2)
  19. {
  20. return 1;
  21. }
  22.  
  23. var result = Fib(n - 1) + Fib(n - 2);
  24.  
  25. numbers[n] = result;
  26.  
  27. return result;
  28. }
  29.  
  30. static void Main()
  31. {
  32. int n = int.Parse(Console.ReadLine());
  33. numbers = new int[n + 1];
  34. int result = Fib(n);
  35. Console.WriteLine(result);
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement