aboutsummaryrefslogtreecommitdiff
path: root/challenge-104/abigail/awk
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-16 15:05:24 +0100
committerAbigail <abigail@abigail.be>2021-03-16 15:05:24 +0100
commitf2e288331b24300dddb937639966f2adbee9969a (patch)
tree175cbdd72cad45f12dc49e429c622edcbd344e32 /challenge-104/abigail/awk
parent42d1cea151fac490af15d06024f9f8563eb23282 (diff)
downloadperlweeklychallenge-club-f2e288331b24300dddb937639966f2adbee9969a.tar.gz
perlweeklychallenge-club-f2e288331b24300dddb937639966f2adbee9969a.tar.bz2
perlweeklychallenge-club-f2e288331b24300dddb937639966f2adbee9969a.zip
Alternative AWK solution for week 104, part 1
Diffstat (limited to 'challenge-104/abigail/awk')
-rw-r--r--challenge-104/abigail/awk/ch-1a.awk44
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-104/abigail/awk/ch-1a.awk b/challenge-104/abigail/awk/ch-1a.awk
new file mode 100644
index 0000000000..4753d404e5
--- /dev/null
+++ b/challenge-104/abigail/awk/ch-1a.awk
@@ -0,0 +1,44 @@
+#!/usr/bin/awk
+
+#
+# See ../README.md
+#
+
+#
+# Run as: awk -f ch-1a.awk < input-file
+#
+
+#
+# Instead of doing the simple thing (print a fixed string), which
+# we've done in ch-1.awk, here we will actually calculate the numbers.
+#
+
+#
+# Initialize the cache
+#
+BEGIN {
+ cache [0] = 0
+ cache [1] = 1
+}
+
+#
+# Calculate the nth FUSC number, using a cache.
+#
+function fusc (n) {
+ if (!(n in cache)) {
+ cache [n] = n % 2 ? fusc((n - 1) / 2) + fusc((n + 1) / 2) \
+ : fusc( n / 2)
+ }
+ return (cache [n])
+}
+
+#
+# Print the first 50 numbers.
+#
+BEGIN {
+ for (i = 0; i < 50; i ++) {
+ printf "%s%d", (i == 0 ? "" : " "), fusc(i)
+ }
+ print
+}
+