aboutsummaryrefslogtreecommitdiff
path: root/challenge-141/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-11-30 20:04:35 +0100
committerAbigail <abigail@abigail.be>2021-11-30 20:04:35 +0100
commit44bcd3ae0c35b887bd579e60c17198360f3497c3 (patch)
tree1fce654d4b0b27996ef85e24d2bafabf7541ffc7 /challenge-141/abigail/ruby
parenta53828c517ea7368d0efc3f64779bd0853c17330 (diff)
downloadperlweeklychallenge-club-44bcd3ae0c35b887bd579e60c17198360f3497c3.tar.gz
perlweeklychallenge-club-44bcd3ae0c35b887bd579e60c17198360f3497c3.tar.bz2
perlweeklychallenge-club-44bcd3ae0c35b887bd579e60c17198360f3497c3.zip
Solutions week 141
Diffstat (limited to 'challenge-141/abigail/ruby')
-rw-r--r--challenge-141/abigail/ruby/ch-1.rb34
-rw-r--r--challenge-141/abigail/ruby/ch-2.rb35
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-141/abigail/ruby/ch-1.rb b/challenge-141/abigail/ruby/ch-1.rb
new file mode 100644
index 0000000000..3aca9a120c
--- /dev/null
+++ b/challenge-141/abigail/ruby/ch-1.rb
@@ -0,0 +1,34 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-1.rb
+#
+
+count = 10
+nr_of_divisors = 8
+
+n = 0
+while count > 0 do
+ n = n + 1
+ s = Math . sqrt(n) . floor()
+ if n == s * s then
+ next
+ end
+ c = 0
+ for d in 1 .. s do
+ if n % d == 0 then
+ c = c + 2
+ if c > nr_of_divisors then
+ break
+ end
+ end
+ end
+ if c == nr_of_divisors then
+ puts (n)
+ count = count - 1
+ end
+end
diff --git a/challenge-141/abigail/ruby/ch-2.rb b/challenge-141/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..86129c8794
--- /dev/null
+++ b/challenge-141/abigail/ruby/ch-2.rb
@@ -0,0 +1,35 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb < input-file
+#
+
+def substrings (n, m, prefix, max)
+ if n . length == 0 then
+ return prefix > -1 &&
+ prefix < max &&
+ prefix % m == 0 ? 1 : 0
+ end
+
+ fc = n[0] . to_i
+ tail = n[1 .. -1]
+ if prefix == -1 then
+ n_prefix = fc
+ else
+ n_prefix = 10 * prefix + fc
+ end
+
+ return substrings(tail, m, n_prefix, max) +
+ substrings(tail, m, prefix, max)
+end
+
+
+ARGF . each_line do
+ | line |
+ n, m = line . strip() . split
+ puts substrings(n, m . to_i, -1, n . to_i)
+end