Advertisement
Guest User

Untitled

a guest
May 27th, 2017
60
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. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Csharp.Advanced.StacksAndQueues
  8. {
  9. public partial class StacksAndQueues
  10. {
  11. public static class _08RecursiveFibonacci
  12. {
  13. public static void Solution()
  14. {
  15. Console.WriteLine(
  16. NthFibonacci(
  17. byte.Parse(Console.ReadLine()),
  18. new Dictionary<ulong, ulong>()
  19. ));
  20. }
  21.  
  22. public static ulong NthFibonacci(ulong nth, Dictionary<ulong, ulong> memoize)
  23. {
  24. if (nth <= 2)
  25. return 1;
  26.  
  27. ulong value;
  28. if (memoize.TryGetValue(nth, out value))
  29. return value;
  30.  
  31. value = NthFibonacci(nth - 1, memoize) + NthFibonacci(nth - 2, memoize);
  32. memoize.Add(nth, value);
  33. return value;
  34. }
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement