Last active
August 8, 2019 03:55
-
-
Save sskset/06bf53d18dabbd5898ebb6a77b9f7d95 to your computer and use it in GitHub Desktop.
fibo
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; | |
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