From 68062715c020b364b86f84f97113f75eb7e3c074 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Thu, 5 Apr 2012 08:10:04 -0400 Subject: [PATCH] First five problems in Ruby and (mostly) simplified to one-liners. --- Ruby/1-a.rb | 10 ++++++++++ Ruby/1-b.rb | 6 ++++++ Ruby/1-c.rb | 3 +++ Ruby/2-a.rb | 11 +++++++++++ Ruby/2-conrad.rb | 3 +++ Ruby/3-a.rb | 4 ++++ Ruby/4-a.rb | 3 +++ Ruby/5-a.rb | 20 ++++++++++++++++++++ Ruby/5-b.rb | 3 +++ 9 files changed, 63 insertions(+) create mode 100755 Ruby/1-a.rb create mode 100755 Ruby/1-b.rb create mode 100755 Ruby/1-c.rb create mode 100755 Ruby/2-a.rb create mode 100755 Ruby/2-conrad.rb create mode 100755 Ruby/3-a.rb create mode 100755 Ruby/4-a.rb create mode 100755 Ruby/5-a.rb create mode 100755 Ruby/5-b.rb diff --git a/Ruby/1-a.rb b/Ruby/1-a.rb new file mode 100755 index 0000000..f0a59e9 --- /dev/null +++ b/Ruby/1-a.rb @@ -0,0 +1,10 @@ +#!/usr/bin/env ruby + +sum = 0; +(1..999).each do |i| + if(i % 3 == 0 || i % 5 == 0) then + sum += i + end +end + +puts sum \ No newline at end of file diff --git a/Ruby/1-b.rb b/Ruby/1-b.rb new file mode 100755 index 0000000..a5f2134 --- /dev/null +++ b/Ruby/1-b.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +sum = 0; +(1..999).each { |i| sum += i if (i % 3 == 0 || i % 5 == 0) } + +puts sum \ No newline at end of file diff --git a/Ruby/1-c.rb b/Ruby/1-c.rb new file mode 100755 index 0000000..cd1f367 --- /dev/null +++ b/Ruby/1-c.rb @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby + +puts (1...1000).select{ |i| i % 3 == 0 || i % 5 == 0 }.inject(:+) \ No newline at end of file diff --git a/Ruby/2-a.rb b/Ruby/2-a.rb new file mode 100755 index 0000000..a7915c7 --- /dev/null +++ b/Ruby/2-a.rb @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby + +sum = 0 +i = j = 1 + +until (i >= 4000000) + i, j = j, i + j + sum += i if(i.even?) +end + +puts sum \ No newline at end of file diff --git a/Ruby/2-conrad.rb b/Ruby/2-conrad.rb new file mode 100755 index 0000000..a83d890 --- /dev/null +++ b/Ruby/2-conrad.rb @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby + +puts Enumerator.new { |yielder| i, j = 0, 1; loop {i, j = j, i + j; yielder.yield i} }.take_while { |n| n <= 4E6}.select { |i| i.even? }.inject(:+) diff --git a/Ruby/3-a.rb b/Ruby/3-a.rb new file mode 100755 index 0000000..92cf52c --- /dev/null +++ b/Ruby/3-a.rb @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby + +require 'prime' +puts Prime.prime_division(600851475143).last[0] \ No newline at end of file diff --git a/Ruby/4-a.rb b/Ruby/4-a.rb new file mode 100755 index 0000000..2518bb5 --- /dev/null +++ b/Ruby/4-a.rb @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby + +p (100...1000).to_a.map{ |i| (100...1000).to_a.map{ |j| i *j } }.flatten(1).select { |i| i.to_s == i.to_s.reverse }.sort.last \ No newline at end of file diff --git a/Ruby/5-a.rb b/Ruby/5-a.rb new file mode 100755 index 0000000..5b33870 --- /dev/null +++ b/Ruby/5-a.rb @@ -0,0 +1,20 @@ +#!/usr/bin/env ruby + +i = 20 +while true do + 20.downto(1) { |j| + break if i % j != 0 + @done = true if j == 1 + } + + break if @done + + if i > 232792560 then + puts 'fail' + break + end + + i += 20 +end + +puts i diff --git a/Ruby/5-b.rb b/Ruby/5-b.rb new file mode 100755 index 0000000..1689d6f --- /dev/null +++ b/Ruby/5-b.rb @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby + + puts (1..20).inject(1) { |lcm, n| lcm.lcm(n) } \ No newline at end of file