aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-01-05 20:35:32 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-01-05 20:35:32 +0100
commitada8b466f3fe8efcf3b85f22495c744517df42ca (patch)
tree1dc6439469d465851a7c36e9ebc75c5f99600307 /challenge-003/abigail/ruby
parentf98a27d3409f2cfd1fdcf283e9453847520869b9 (diff)
downloadperlweeklychallenge-club-ada8b466f3fe8efcf3b85f22495c744517df42ca.tar.gz
perlweeklychallenge-club-ada8b466f3fe8efcf3b85f22495c744517df42ca.tar.bz2
perlweeklychallenge-club-ada8b466f3fe8efcf3b85f22495c744517df42ca.zip
Week 3: copied solutions from week 123.
Week 123 has exactly the same challenge. Since I prefer that solution, I copied those results. Tests adjusted accordingly.
Diffstat (limited to 'challenge-003/abigail/ruby')
-rw-r--r--challenge-003/abigail/ruby/ch-1.rb35
1 files changed, 19 insertions, 16 deletions
diff --git a/challenge-003/abigail/ruby/ch-1.rb b/challenge-003/abigail/ruby/ch-1.rb
index c0a22397b0..d07d858bda 100644
--- a/challenge-003/abigail/ruby/ch-1.rb
+++ b/challenge-003/abigail/ruby/ch-1.rb
@@ -1,26 +1,29 @@
#!/usr/bin/ruby
#
-# See ../README.md
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003
#
#
# Run as: ruby ch-1.rb < input-file
#
-ARGF . each_line do |_|
- max = _ . to_i
- base2 = 1
- while base2 <= max
- base3 = base2
- while base3 <= max
- base5 = base3
- while base5 <= max
- puts base5
- base5 *= 5
- end
- base3 *= 3
- end
- base2 *= 2
- end
+ugly = [1]
+next_2 = 0
+next_3 = 0
+next_5 = 0
+
+ARGF . each_line do
+ |n|
+ n = n . to_i
+ while ugly . length < n do
+ ugly . push ([2 * ugly [next_2],
+ 3 * ugly [next_3],
+ 5 * ugly [next_5]] . min)
+
+ next_2 += 1 if 2 * ugly [next_2] <= ugly [-1]
+ next_3 += 1 if 3 * ugly [next_3] <= ugly [-1]
+ next_5 += 1 if 5 * ugly [next_5] <= ugly [-1]
+ end
+ puts (ugly [n - 1])
end