From 78b8eff4fd091098bf6cba2ec9bcfb1ac145166c Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 18 Mar 2021 00:17:59 +0100 Subject: Alternative C solution for week 104, part 1 --- challenge-104/abigail/README.md | 4 ++- challenge-104/abigail/c/ch-1a.c | 54 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 challenge-104/abigail/c/ch-1a.c 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 +# include +# include + +/* + * 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); +} -- cgit