summaryrefslogtreecommitdiff
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
add week 1
-rw-r--r--license21
-rw-r--r--week1/Collatz.cs25
-rw-r--r--week1/Fibonacci.cs28
-rw-r--r--week1/PyramidProblem.cs22
-rw-r--r--week1/StringReverse.cs19
5 files changed, 115 insertions, 0 deletions
diff --git a/license b/license
new file mode 100644
index 0000000..5fe3518
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 Loek Le Blansch
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
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;
+ }
+ }
+}
+