Aug
11
In my attempt to learn Ruby out in the open, here’s my solution for Project Euler Problem 36.
As Jon promised, the mid-30s problems on Project Euler are easy to solve. It’s nice that integer to binary conversation is built into the Ruby language. That helped a bit.
As always, any feedback is welcome.
# Euler 36
# http://projecteuler.net/index.php?section=problems&id=36
# The decimal number, 585 = 1001001001 (binary), is
# palindromic in both bases.
# Find the sum of all numbers, less than one million,
# which are palindromic in base 10 and base 2.
#
# (Please note that the palindromic number, in either base,
# may not include leading zeros.)
timer_start = Time.now
def is_palindrome?(n)
n.to_s == n.to_s.reverse
end
puts (0..999999) \
.select { |n| is_palindrome?(n) && is_palindrome?(n.to_s(2))} \
.inject { |agg, n| agg + n }
puts "Elapsed Time: #{(Time.now - timer_start)*1000} milliseconds"