Skip to content

Instantly share code, notes, and snippets.

@UdaraAlwis
Created July 26, 2020 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UdaraAlwis/0c951827231470d8e2ede48152419986 to your computer and use it in GitHub Desktop.
Save UdaraAlwis/0c951827231470d8e2ede48152419986 to your computer and use it in GitHub Desktop.
Solving Fibonacci Series with Dynamic Programming in C#
using System;
using System.Diagnostics;
// Solving Fibonacci series with Dynamic
// Programming algorithms using C#
public class Program
{
public static void Main()
{
Console.WriteLine("Solving Fibonacci Series with Dynamic Programming in C#");
Console.WriteLine("=======================================================");
Console.WriteLine("==================BY==UDARA ALWIS======================");
Console.WriteLine("=======================================================");
Console.WriteLine("");
Console.WriteLine("Recursive method");
Stopwatch stopwatch = Stopwatch.StartNew();
// Recursive
for (int i = 0; i < 30; i++)
{
Console.Write(GetFibValueOf_Recursive(i + 1).ToString() + ",");
}
stopwatch.Stop();
Console.WriteLine("\nTime elapsed: {0}", stopwatch.ElapsedTicks);
Console.WriteLine();
Console.WriteLine("Memoize method");
stopwatch = Stopwatch.StartNew();
// Memoize
for (int i = 0; i < 30; i++)
{
int? [] memoList = new int? [i + 1];
Console.Write(GetFibValueOf_Memoize(i + 1, memoList).ToString() + ",");
}
stopwatch.Stop();
Console.WriteLine("\nTime elapsed: {0}", stopwatch.ElapsedTicks);
Console.WriteLine();
Console.WriteLine("Bottom Up method");
stopwatch = Stopwatch.StartNew();
// Bottom Up
for (int i = 0; i < 30; i++)
{
Console.Write(GetFibValueOf_BottomUp(i + 1).ToString() + ",");
}
stopwatch.Stop();
Console.WriteLine("\nTime elapsed: {0}", stopwatch.ElapsedTicks);
Console.WriteLine();
}
private static int GetFibValueOf_Recursive(int position)
{
if (position == 1 || position == 2)
return 1;
return GetFibValueOf_Recursive(position - 1) + GetFibValueOf_Recursive(position - 2);
}
private static int? GetFibValueOf_Memoize(int position, int? [] memoList)
{
if (memoList[position - 1] != null)
{
return memoList[position - 1];
}
int? result = 0;
if (position == 1 || position == 2)
result = memoList[position - 1] = 1;
else
{
result = memoList[position - 1] = GetFibValueOf_Memoize(position - 1, memoList) + GetFibValueOf_Memoize(position - 2, memoList);
}
return result;
}
private static int? GetFibValueOf_BottomUp(int position)
{
if(position == 1 || position == 2)
return 1;
else
{
int[] fibArray = new int[position];
fibArray[0] = 1;
fibArray[1] = 1;
for(int i = 2; i < position; i++)
{
fibArray[i] = (fibArray[i - 1]) + (fibArray[i - 2]);
}
return fibArray[position - 1];
}
}
}
// RESULTS:
// Solving Fibonachi Series with Dynamic Programming in C#
// =======================================================
// ==================BY==UDARA ALWIS======================
// =======================================================
//
// Recursive method
// 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,
// Time elapsed: 271522
//
// Memoize method
// 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,
// Time elapsed: 3608
//
// Bottom Up method
// 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,
// Time elapsed: 2318
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment