Problem 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.)
[code=ruby]def palindromic(upto)
(1…upto).each do |num|
yield num if num.to_s == num.to_s.reverse && num.to_s(2) == num.to_s(2).reverse
end
end
puts enum_for(:palindromic, 1_000_000).reduce(:+)[/code]
teamon
3
p (1..1_000_000).select {|e| [e.to_s, e.to_s(2)].all? {|i| i == i.reverse} }.inject(:+)
timmy
4
p (1..999999).select {|a| a.to_s == a.to_s.reverse && a.to_s(2) == a.to_s(2).reverse}.inject {|sum, n| sum + n}