diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2022-01-16 19:03:20 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2022-01-16 19:15:13 +0100 |
| commit | 0cbed3d1cd8b789156d383d62f25a2ebaff00371 (patch) | |
| tree | 61cfba3f331c55b0911ac6b979ad927a96256fa8 /challenge-147/abigail/ruby | |
| parent | 6a42ef0f1bfd7b33cd271aff9a0323cf97620bad (diff) | |
| download | perlweeklychallenge-club-0cbed3d1cd8b789156d383d62f25a2ebaff00371.tar.gz perlweeklychallenge-club-0cbed3d1cd8b789156d383d62f25a2ebaff00371.tar.bz2 perlweeklychallenge-club-0cbed3d1cd8b789156d383d62f25a2ebaff00371.zip | |
Week 147: Ruby solutions
Diffstat (limited to 'challenge-147/abigail/ruby')
| -rw-r--r-- | challenge-147/abigail/ruby/ch-1.rb | 49 | ||||
| -rw-r--r-- | challenge-147/abigail/ruby/ch-2.rb | 30 |
2 files changed, 79 insertions, 0 deletions
diff --git a/challenge-147/abigail/ruby/ch-1.rb b/challenge-147/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..0e0eaffc4b --- /dev/null +++ b/challenge-147/abigail/ruby/ch-1.rb @@ -0,0 +1,49 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: ruby ch-1.rb +# + +require 'prime' + +todo = [2, 3, 5, 7]; + +todo . each do + | x | + print (x . to_s + " ") +end + +count = 20 - todo . length + +pow = 10 +while todo . length > 0 && count > 0 do + new_todo = [] + for d in 1 .. 9 do + todo . each do + | p | + candidate = d * pow + p + if (Prime . prime? (candidate)) then + new_todo . push (candidate) + print (candidate . to_s + " ") + count = count - 1 + if (count <= 0) then + break + end + end + if (count <= 0) then + break + end + end + if (count <= 0) then + break + end + end + pow = pow * 10 + todo = new_todo +end + +puts ("") diff --git a/challenge-147/abigail/ruby/ch-2.rb b/challenge-147/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..a3dbb601cc --- /dev/null +++ b/challenge-147/abigail/ruby/ch-2.rb @@ -0,0 +1,30 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +# + +# +# Run as: ruby ch-2.rb +# + +pentagon = {} +p = 0 +n = 0 +done = false + + +while not done do + p = p + n + n + n + 1 + n = n + 1 + pentagon [p] = true + pentagon . each do + | seen, _ | + if (seen + seen <= p && pentagon . key?(p - seen) \ + && pentagon . key?(p - seen - seen)) then + puts (seen . to_s + " " + (p - seen) . to_s) + done = true + break + end + end +end |
