From 973a9b7fe9c34e3a06b5caf0eb764fa55ff615df Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 9 Sep 2024 10:40:25 +0200 Subject: add week 1 --- week1/Collatz.cs | 25 +++++++++++++++++++++++++ week1/Fibonacci.cs | 28 ++++++++++++++++++++++++++++ week1/PyramidProblem.cs | 22 ++++++++++++++++++++++ week1/StringReverse.cs | 19 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 week1/Collatz.cs create mode 100644 week1/Fibonacci.cs create mode 100644 week1/PyramidProblem.cs create mode 100644 week1/StringReverse.cs (limited to 'week1') 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; + } + } +} + -- cgit v1.2.3