summaryrefslogtreecommitdiff
path: root/week1/Fibonacci.cs
blob: f5d7a4dc8897e79b83012cad39ba6266ebe62f6b (plain)
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
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;
		}
	}
}