Created
May 27, 2017 14:14
-
-
Save Tangrila-BG/956c49902ac955ffc97ac2021729a414 to your computer and use it in GitHub Desktop.
with memoization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Csharp.Advanced.StacksAndQueues | |
{ | |
public partial class StacksAndQueues | |
{ | |
public static class _08RecursiveFibonacci | |
{ | |
public static void Solution() | |
{ | |
Console.WriteLine( | |
NthFibonacci( | |
byte.Parse(Console.ReadLine()), | |
new Dictionary<ulong, ulong>() | |
)); | |
} | |
public static ulong NthFibonacci(ulong nth, Dictionary<ulong, ulong> memoize) | |
{ | |
if (nth <= 2) | |
return 1; | |
ulong value; | |
if (memoize.TryGetValue(nth, out value)) | |
return value; | |
value = NthFibonacci(nth - 1, memoize) + NthFibonacci(nth - 2, memoize); | |
memoize.Add(nth, value); | |
return value; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment