Advertisement
desislava_topuzakova

Факториел + Фибоначи

Jun 7th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace AlgorithmsOverStructureData
  6. {
  7. class Program
  8. {
  9. static Dictionary<int> resultHistory = new Dictionary<int>();
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine(Factorial(5)); //5! = 5 * 4 * 3 * 2 * 1
  13. Console.WriteLine(FibonacciRecursiveOpt(6));
  14.  
  15. }
  16.  
  17. static long Factorial(int num)
  18. {
  19. if (num == 0)
  20. {
  21. return 1;
  22. }
  23. return num * Factorial(num - 1);
  24. }
  25.  
  26.  
  27. static int FibonacciRecursiveOpt(int n)
  28. {
  29. if (n == 0) return 0;
  30. if (n == 1) return 1;
  31. if (resultHistory.ContainsKey(n))
  32. return resultHistory[n];
  33.  
  34. int result = FibonacciRecursiveOpt(n - 1) + FibonacciRecursiveOpt(n - 2);
  35. resultHistory[n] = result;
  36.  
  37. return result;
  38. }
  39.  
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement