Skip to content

Instantly share code, notes, and snippets.

@Tangrila-BG
Created May 27, 2017 14:14
Show Gist options
  • Save Tangrila-BG/956c49902ac955ffc97ac2021729a414 to your computer and use it in GitHub Desktop.
Save Tangrila-BG/956c49902ac955ffc97ac2021729a414 to your computer and use it in GitHub Desktop.
with memoization
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