aboutsummaryrefslogtreecommitdiff
path: root/challenge-104/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-18 19:53:37 +0100
committerAbigail <abigail@abigail.be>2021-03-18 19:53:37 +0100
commit8e4529e42ce886908d7efd93bd43d2fd9046ccf7 (patch)
treeecf810099c170e8aebf0bb101db095b1354294e2 /challenge-104/abigail/ruby
parent97e674ad1a41dc984257ee5f6587758f1275b710 (diff)
downloadperlweeklychallenge-club-8e4529e42ce886908d7efd93bd43d2fd9046ccf7.tar.gz
perlweeklychallenge-club-8e4529e42ce886908d7efd93bd43d2fd9046ccf7.tar.bz2
perlweeklychallenge-club-8e4529e42ce886908d7efd93bd43d2fd9046ccf7.zip
Alternative Ruby solution for week 104, part 1
Diffstat (limited to 'challenge-104/abigail/ruby')
-rw-r--r--challenge-104/abigail/ruby/ch-1a.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-104/abigail/ruby/ch-1a.rb b/challenge-104/abigail/ruby/ch-1a.rb
new file mode 100644
index 0000000000..2203a42cfa
--- /dev/null
+++ b/challenge-104/abigail/ruby/ch-1a.rb
@@ -0,0 +1,45 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-1a.rb
+#
+
+#
+# Initialize the cache. For some reason, this needs to be a global variable.
+#
+$cache = Hash . new
+$cache [0] = 0
+$cache [1] = 1
+max = 50
+
+#
+# Calculate the nth element of the FUSC sequence.
+#
+def fusc (n)
+ if !($cache . has_value? (n))
+ if n % 2 == 1
+ $cache [n] = (fusc ((n - 1) / 2)) + (fusc ((n + 1) / 2))
+ else
+ $cache [n] = fusc (n / 2)
+ end
+ end
+
+ return $cache [n]
+end
+
+
+#
+# Print the first 50 values of the FUSC sequence.
+#
+for n in 0 .. (max - 1) do
+ if n > 0
+ print (" ")
+ end
+ print (fusc (n))
+end
+
+print ("\n")