Last active
August 29, 2015 14:10
-
-
Save trushkevich/8d476b1704c607022a7a to your computer and use it in GitHub Desktop.
Friendly coding competition - ranges, palindromes etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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