diff options
Diffstat (limited to 'week1/Fibonacci.cs')
-rw-r--r-- | week1/Fibonacci.cs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/week1/Fibonacci.cs b/week1/Fibonacci.cs new file mode 100644 index 0000000..f5d7a4d --- /dev/null +++ b/week1/Fibonacci.cs @@ -0,0 +1,28 @@ +namespace ALGA { + public class Fibonacci { + public static int fibonacci_recursive(int n) { + if (n <= 0) return 0; + if (n == 1) return 1; + return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2); + } + + public static int fibonacci_iterative(int n) { + if (n < 0) return 0; + int a = 0, b = 1; + for (int i = 0; i < n; i++) + (a, b) = (b, a + b); + return a; + } + + public enum Answer { IterativeIsFaster, RecursiveIsFaster }; + + public static Answer which_is_faster() { + /** + * Iterative is 'sneller' omdat deze O(n) is in tegenstelling tot de + * recursieve versie die O(2^n) is + */ + return Answer.IterativeIsFaster; + } + } +} + |