Nth FIBONACCI NUMBER (DYNAMIC PROGRAMMING METHOD)
//Print Nth fibonacci number
namespace FibonacciDynamicProgramming
{
class Program
{
static int Fib(int n, Dictionary<int,int> DP)
{
if (DP.ContainsKey(n)) return DP[n];
int num1 = Fib(n - 1, DP);
int num2 = Fib(n - 2, DP);
DP[n] = num1 + num2;
return DP[n];
}
static void Main(string[] args)
{
Dictionary<int, int> DP = new Dictionary<int, int>();
DP[0] = 0;
DP[1] = 1;
Console.WriteLine(Fib(5, DP));
}
}
}
No comments:
Post a Comment