From 610ff9605ec7b308051a8f04f9b21d6f4060a8cd Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 19 Apr 2021 20:16:47 +0200 Subject: Solutions in 29 languages for week 109, part 1. --- challenge-109/abigail/ruby/ch-1.rb | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 challenge-109/abigail/ruby/ch-1.rb (limited to 'challenge-109/abigail/ruby') diff --git a/challenge-109/abigail/ruby/ch-1.rb b/challenge-109/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..95a38a69cb --- /dev/null +++ b/challenge-109/abigail/ruby/ch-1.rb @@ -0,0 +1,44 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb [plain | compute] +# + +PLAIN = 0 +COMPUTE = 1 + +COUNT = 20 + +def divisor_sum (n) + sum = 0 + for i in 2 .. n - 1 + if n % i == 0 + then sum += i + end + end + return sum +end + +type = PLAIN + +if ARGV . length > 0 && ARGV[0] == "compute" +then type = COMPUTE +end + +if type == PLAIN +then puts ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21"); +end + +if type == COMPUTE +then for n in 1 .. COUNT + if n > 1 + then print (", ") + end + print divisor_sum (n) + end + puts ("") +end -- cgit From 7b4fbbe6505b63d2faa62ad34939b6e830ea692b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 20 Apr 2021 20:52:57 +0200 Subject: When looping over possible divisors, stop at n / 2. --- challenge-109/abigail/ruby/ch-1.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-109/abigail/ruby') diff --git a/challenge-109/abigail/ruby/ch-1.rb b/challenge-109/abigail/ruby/ch-1.rb index 95a38a69cb..0c73c368dd 100644 --- a/challenge-109/abigail/ruby/ch-1.rb +++ b/challenge-109/abigail/ruby/ch-1.rb @@ -15,7 +15,7 @@ COUNT = 20 def divisor_sum (n) sum = 0 - for i in 2 .. n - 1 + for i in 2 .. n / 2 if n % i == 0 then sum += i end -- cgit From 078dca365480326018fc5482e7c465a9e77824c4 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 24 Apr 2021 18:46:41 +0200 Subject: Node.js and Ruby solutions for week 109, part 2 --- challenge-109/abigail/ruby/ch-2.rb | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 challenge-109/abigail/ruby/ch-2.rb (limited to 'challenge-109/abigail/ruby') diff --git a/challenge-109/abigail/ruby/ch-2.rb b/challenge-109/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..faaa7b8489 --- /dev/null +++ b/challenge-109/abigail/ruby/ch-2.rb @@ -0,0 +1,48 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +SIZE = 7 + +# +# Brute force all possibilities, with early returns. +# +ARGF . each_line do |_| + numbers = (_ . split) . map {|_| _ . to_i} + + numbers . each_with_index do |a, a_i| + numbers . each_with_index do |b, b_i| + next if a_i == b_i + target = a + b + numbers . each_with_index do |c, c_i| + next if a_i == c_i || b_i == c_i + numbers . each_with_index do |d, d_i| + next if a_i == d_i || b_i == d_i || c_i == d_i + next if target != b + c + d + numbers . each_with_index do |e, e_i| + next if a_i == e_i || b_i == e_i || c_i == e_i || + d_i == e_i + numbers . each_with_index do |f, f_i| + next if a_i == f_i || b_i == f_i || c_i == f_i || + d_i == f_i || e_i == f_i + next if target != d + e + f + numbers . each_with_index do |g, g_i| + next if a_i == g_i || b_i == g_i || + c_i == g_i || d_i == g_i || + e_i == g_i || f_i == g_i + next if target != f + g + puts "#{a} #{b} #{c} #{d} #{e} #{f} #{g}" + end + end + end + end + end + end + end +end -- cgit