aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-18 00:17:59 +0100
committerAbigail <abigail@abigail.be>2021-03-18 00:19:05 +0100
commit78b8eff4fd091098bf6cba2ec9bcfb1ac145166c (patch)
treee980c191a147afe30274418fca9ff3869e25214a
parentde5f61d242597618622ab9d377d7fa6eca0f8c02 (diff)
downloadperlweeklychallenge-club-78b8eff4fd091098bf6cba2ec9bcfb1ac145166c.tar.gz
perlweeklychallenge-club-78b8eff4fd091098bf6cba2ec9bcfb1ac145166c.tar.bz2
perlweeklychallenge-club-78b8eff4fd091098bf6cba2ec9bcfb1ac145166c.zip
Alternative C solution for week 104, part 1
-rw-r--r--challenge-104/abigail/README.md4
-rw-r--r--challenge-104/abigail/c/ch-1a.c54
2 files changed, 57 insertions, 1 deletions
diff --git a/challenge-104/abigail/README.md b/challenge-104/abigail/README.md
index e85ff0d471..f4c74aad05 100644
--- a/challenge-104/abigail/README.md
+++ b/challenge-104/abigail/README.md
@@ -30,7 +30,9 @@ the solution.
* [Basic](basic/ch-1.bas)
* [Befunge-93](befunge-93/ch-1.bf93)
* [bc](bc/ch-1.bc)
-* [C](c/ch-1.c)
+* C
+ * [Simple](c/ch-1.c)
+ * [Calculated](c/ch-1a.c)
* [Cobol](cobol/ch-1.cb)
* [C-shell](csh/ch-1.csh)
* [Erlang](erlang/ch-1.erl)
diff --git a/challenge-104/abigail/c/ch-1a.c b/challenge-104/abigail/c/ch-1a.c
new file mode 100644
index 0000000000..f0a2c15bdf
--- /dev/null
+++ b/challenge-104/abigail/c/ch-1a.c
@@ -0,0 +1,54 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+
+/*
+ * See ../README.md
+ */
+
+/*
+ * Run as: cc -o ch-1a.o ch-1a.c; ./ch-1a.o
+ */
+
+# define MAX 50
+
+typedef long long number;
+number cache [MAX];
+
+/*
+ * Fusc sequence is defined as:
+ * ( n, 0 <= n <= 1
+ * fusc (n) = { fusc (n / 2), n > 1 && n even
+ * ( fusc ((n - 1) / 2) + fusc ((n + 1) / 2), n > 1 && n odd
+ */
+
+number fusc (size_t n) {
+ if (cache [n] > -1) {
+ return cache [n];
+ }
+ cache [n] = n % 2 ? fusc ((n - 1) / 2) + fusc ((n + 1) / 2)
+ : fusc ( n / 2);
+ return cache [n];
+}
+
+int main (void) {
+ /*
+ * Initialize the cache
+ */
+ for (size_t i = 0; i < MAX; i ++) {
+ cache [i] = -1;
+ }
+ cache [0] = 0;
+ cache [1] = 1;
+
+ /*
+ * Calculate the values and print them.
+ */
+ for (size_t i = 0; i < MAX; i ++) {
+ printf ("%s%lld", i == 0 ? "" : " ", fusc (i));
+ }
+
+ printf ("\n");
+
+ return (0);
+}