← Back to davo.co
aboutsummaryrefslogtreecommitdiffstats
path: root/fibonacci4.c
diff options
context:
space:
mode:
Diffstat (limited to 'fibonacci4.c')
-rw-r--r--fibonacci4.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/fibonacci4.c b/fibonacci4.c
new file mode 100644
index 0000000..ff250c7
--- /dev/null
+++ b/fibonacci4.c
@@ -0,0 +1,30 @@
+#include "c23-fallback.h"
+#include <stdio.h>
+
+/**
+ ** Rewrite the recursive Fibonacci such that it alternates the use of
+ ** the buffers.
+ **/
+
+void fib2rec(size_t n, size_t buf[2]) {
+ if (n) {
+ buf[n%2] += buf[!(n%2)];
+ fib2rec(n-1, buf);
+ }
+}
+
+
+size_t fib2(size_t n) {
+ size_t res[2] = { 1, 1, };
+ if (n > 2) fib2rec(n - 2, res);
+ return res[1];
+}
+
+int main(int argc, char* argv[argc+1]) {
+ for (int i = 1; i < argc; ++i) { // process args
+ size_t const n = strtoull(argv[i], nullptr, 0); // arg -> size_t
+ printf("fib(%zu) is %zu\n",
+ n, fib2(n));
+ }
+ return EXIT_SUCCESS;
+}