-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (75 loc) · 4.12 KB
/
Program.cs
File metadata and controls
115 lines (75 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class Fibonacci
{
static int[] dp = new int[0]; // public int array to store previous fibonacci calculations (like a inMemory cache)
static int interactionsCountSimpleMethod;
static int interactionsCountTailRecursiveMethod;
static int interactionsCountDynamicProgrammingMethod;
public static void Main()
{
DateTime start;
int result;
TimeSpan duration;
//Calling Fibonacci calculation comparing 3 different methods for Fib(42) until Fib(38)
for (int n = 42; n >= 38; n--)
{
interactionsCountDynamicProgrammingMethod = 0;
interactionsCountTailRecursiveMethod = 0;
interactionsCountSimpleMethod = 0;
Console.WriteLine( Environment.NewLine + $"Processing Fibonacci({n}) ...");
//CalculateFibonacciSimpleMethod() --> Slow, recursive, exponential, intuitive
start = DateTime.Now;
result = CalculateFibonacciSimpleMethod(n);
duration = DateTime.Now - start;
Console.WriteLine($"CalculateFibonacciSimpleMethod ({n}) is ---> {result}. Execution time: {duration.TotalMilliseconds.ToString().PadLeft(15)}ms and {interactionsCountSimpleMethod} interactions");
//CalculateFibonacciDynamicProgrammingMethod() --> fast, flat, linear, intuitive, cached for subsequent requests
start = DateTime.Now;
result = CalculateFibonacciDynamicProgrammingMethod(n);
duration = DateTime.Now - start;
Console.WriteLine($"CalculateFibonacciDynamicProgrammingMethod ({n}) is ---> {result}. Execution time: {duration.TotalMilliseconds.ToString().PadLeft(15)}ms and {interactionsCountDynamicProgrammingMethod} interactions");
//CalculateFibonacciTailRecursiveMethod() --> fast, linear, recursive, less intuitive
start = DateTime.Now;
result = CalculateFibonacciTailRecursiveMethod(n);
duration = DateTime.Now - start;
Console.WriteLine($"CalculateFibonacciTailRecursiveMethod ({n}) is ---> {result}. Execution time: {duration.TotalMilliseconds.ToString().PadLeft(15)}ms and {interactionsCountTailRecursiveMethod} interactions");
}
}
public static int CalculateFibonacciSimpleMethod(int n)
{
interactionsCountSimpleMethod++; //used to show in the log how many loops this recursive method did
if (n < 2) { return n; }
//Simple, recursive, exponential and the bad way to do Fibonacci calculation
return CalculateFibonacciSimpleMethod(n - 1) + CalculateFibonacciSimpleMethod(n - 2);
}
public static int CalculateFibonacciDynamicProgrammingMethod(int n)
{
interactionsCountDynamicProgrammingMethod++; //used to show in the log how many loops this method did
if (n == 0) return 0;
// If previous calculation greater than "n" was already stored in the array, returns the stored Fibonacci calculation from memory array
if (dp.Length > n && dp[n] > 0)
{
return dp[n]; // return from memory array
}
dp = new int[n + 1];
//base cases
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++)
{
interactionsCountDynamicProgrammingMethod++; //used to show in the log how many loops this method did
dp[i] = dp[i - 1] + dp[i - 2];
if (dp[i] < 0) //happens when int+int excceed int maxValeu and C# returns a negative val
{
throw new Exception($"Fibonacci {i}th results an invalid calculation because it is too long to be stored in an integer variable");
}
}
return dp[n];
}
public static int CalculateFibonacciTailRecursiveMethod(int n, int previous = 0, int current = 1)
{
interactionsCountTailRecursiveMethod++; //used to show in the log how many loops this recursive method did
if (n == 0) { return previous; }
if (n == 1) { return current; }
return CalculateFibonacciTailRecursiveMethod(n - 1, current, previous + current);
}
}