aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-01-23 17:48:33 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-01-23 17:48:33 +0100
commit577e48ca0393ab9c4c9f96d3872867262ec994e9 (patch)
tree0dc039cfa77f18d2454243e11356873677fcdb74
parent13af2d474b88031642215c3bbb35585c8f7020d7 (diff)
downloadperlweeklychallenge-club-577e48ca0393ab9c4c9f96d3872867262ec994e9.tar.gz
perlweeklychallenge-club-577e48ca0393ab9c4c9f96d3872867262ec994e9.tar.bz2
perlweeklychallenge-club-577e48ca0393ab9c4c9f96d3872867262ec994e9.zip
Week 148, part 2: Ruby solution
-rw-r--r--challenge-148/abigail/ruby/ch-2.rb95
1 files changed, 95 insertions, 0 deletions
diff --git a/challenge-148/abigail/ruby/ch-2.rb b/challenge-148/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..860f1ff1b7
--- /dev/null
+++ b/challenge-148/abigail/ruby/ch-2.rb
@@ -0,0 +1,95 @@
+#!/usr/bin/ruby
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-148
+#
+
+#
+# Run as: ruby ch-2.rb
+#
+
+count = 5
+out = []
+a_i = 0
+b_i = 1
+c_i = 2
+sum_i = 3
+for i in 0 .. count - 1 do
+ out . push ([999999, 999999, 999999, 999999 * 3])
+end
+
+max_index = 0
+k = 0
+
+while 3 * k + 2 < out [max_index] [sum_i] do
+ a = 3 * k + 2
+ f1 = k + 1
+ f2 = 8 * k + 5
+
+ d1 = []
+ for i in 1 .. f1 do
+ if i * i > f1 then
+ next
+ end
+ if f1 % i == 0 then
+ d1 . push (i)
+ if i != f1 / i then
+ d1 . push (f1 / i)
+ end
+ end
+ end
+
+ d2 = []
+ for i in 1 .. f2 do
+ if i * i > f2 then
+ next
+ end
+ if f2 % i == 0 then
+ s1 = Math . sqrt(i) . floor()
+ s2 = Math . sqrt(f2 / i) . floor()
+ if s1 * s1 == i then
+ d2 . push (s1)
+ end
+ if (s2 * s2 == f2 / i) && s1 != s2 then
+ d2 . push (s2)
+ end
+ end
+ end
+
+ d1 . each do |d1v|
+ d2 . each do |d2v|
+ b = d1v * d2v
+ c = f1 * f1 * f2 / (b * b)
+ if a + b + c < out [max_index] [sum_i] then
+ seen = false
+ out . each do |entry|
+ if entry [a_i] == a && entry [b_i] == b then
+ seen = true
+ end
+ end
+ if seen then
+ next
+ end
+
+ out [max_index] = [a, b, c, a + b + c]
+
+ max_index = 0
+ max_sum = out [max_index] [sum_i]
+ for i in 1 .. count - 1 do
+ if max_sum < out [i] [sum_i] then
+ max_index = i
+ max_sum = out [i] [sum_i]
+ end
+ end
+ end
+ end
+ end
+ k = k + 1
+ if k > 10 then
+ break
+ end
+end
+
+out . each do |entry|
+ printf "%d %d %d\n", entry [a_i], entry [b_i], entry [c_i]
+end