Skip to content

Instantly share code, notes, and snippets.

@trushkevich
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trushkevich/8d476b1704c607022a7a to your computer and use it in GitHub Desktop.
Save trushkevich/8d476b1704c607022a7a to your computer and use it in GitHub Desktop.
Friendly coding competition - ranges, palindromes etc
#!/usr/bin/env ruby
class Runner
attr_accessor :min, :max, :sum
def initialize(min, max)
@sum = 0
@min, @max = min, max
end
def run
current = @min + (@min.odd? ? 0 : 1)
while current <= @max
string = current.to_s
if string == string.reverse
string_b = current.to_s(2)
if string_b == string_b.reverse
# puts current
@sum += current
end
end
current += 2
end
puts @sum
end
end
# t1 = Time.now
Runner.new(*ARGV.first.split(/\.+/).map(&:to_i)).run
# puts "Time spent: #{(Time.now - t1).round(2)} s."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment