summaryrefslogtreecommitdiff
path: root/week1
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-09-09 10:40:25 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-09-09 10:40:25 +0200
commit973a9b7fe9c34e3a06b5caf0eb764fa55ff615df (patch)
treebffdda05eefd01e5d05dc7a8355dc38764929573 /week1
add week 1
Diffstat (limited to 'week1')
-rw-r--r--week1/Collatz.cs25
-rw-r--r--week1/Fibonacci.cs28
-rw-r--r--week1/PyramidProblem.cs22
-rw-r--r--week1/StringReverse.cs19
4 files changed, 94 insertions, 0 deletions
diff --git a/week1/Collatz.cs b/week1/Collatz.cs
new file mode 100644
index 0000000..556a4c4
--- /dev/null
+++ b/week1/Collatz.cs
@@ -0,0 +1,25 @@
+namespace ALGA {
+ public class Collatz {
+ public static int collatz_recursive(int n) {
+ if (n <= 0) return -1;
+ if (n == 1) return 0;
+ if (n % 2 == 0) n /= 2;
+ else n = n * 3 + 1;
+ return 1 + collatz_recursive(n);
+ }
+
+ public static int collatz_iterative(int n) {
+ if (n <= 0) return -1;
+ if (n == 1) return 0;
+
+ int steps = 0;
+ while (n != 1) {
+ if (n % 2 == 0) n /= 2;
+ else n = n * 3 + 1;
+ steps++;
+ }
+ return steps;
+ }
+ }
+}
+
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;
+ }
+ }
+}
+
diff --git a/week1/PyramidProblem.cs b/week1/PyramidProblem.cs
new file mode 100644
index 0000000..1be67d9
--- /dev/null
+++ b/week1/PyramidProblem.cs
@@ -0,0 +1,22 @@
+namespace ALGA {
+ public class PyramidProblem {
+ public static int triangular_number_recursive(int n) {
+ if (n <= 0) return 0;
+ return n + triangular_number_recursive(n - 1);
+ }
+
+ public static int triangular_number_iterative(int n) {
+ if (n <= 0) return 0;
+ int total = 0;
+ for (int i = 1; i <= n; i++)
+ total += i;
+ return total;
+ }
+
+ public static int triangular_number_function(int n) {
+ if (n <= 0) return 0;
+ return (n + 1) * n / 2;
+ }
+ }
+}
+
diff --git a/week1/StringReverse.cs b/week1/StringReverse.cs
new file mode 100644
index 0000000..2d09b0f
--- /dev/null
+++ b/week1/StringReverse.cs
@@ -0,0 +1,19 @@
+using System;
+
+namespace ALGA {
+ public class StringReverse {
+ public static String string_reverse(String s) {
+ if (s.Length == 0) return s;
+ return s[s.Length - 1] + string_reverse(s.Substring(0, s.Length - 1));
+ }
+
+ public static bool is_palindrome(String s) {
+ for (int front = 0; front < s.Length; front++) {
+ int back = s.Length - 1 - front;
+ if (s[front] != s[back]) return false;
+ }
+ return true;
+ }
+ }
+}
+