Skip to content

Instantly share code, notes, and snippets.

@sskset
Last active August 8, 2019 03:55
Show Gist options
  • Save sskset/06bf53d18dabbd5898ebb6a77b9f7d95 to your computer and use it in GitHub Desktop.
Save sskset/06bf53d18dabbd5898ebb6a77b9f7d95 to your computer and use it in GitHub Desktop.
fibo
using System;
using System.Collections.Generic;
namespace ConsoleApp4
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(F(21));
Console.WriteLine();
foreach(var item in resultLookup)
{
Console.WriteLine($"{item.Key.ToString("00")} - {item.Value}");
}
Console.ReadKey();
}
private static Dictionary<int, int> resultLookup =
new Dictionary<int, int>(
new List<KeyValuePair<int, int>>() {
new KeyValuePair<int, int>(1, 1),
new KeyValuePair<int, int>(2, 2) });
private static int F(int n)
{
if (n <= 0)
{
throw new ArgumentOutOfRangeException(nameof(n));
}
if (!resultLookup.ContainsKey(n))
{
int currentIndex = resultLookup.Count + 1;
do
{
resultLookup.Add(currentIndex, resultLookup[currentIndex - 1] + resultLookup[currentIndex - 2]);
} while (++currentIndex <= n);
}
return resultLookup[n];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment