Project Euler Problem 36

問題

The decimal number, 585 = 1001001001_(2) (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.)

ソース

class Integer
	def to_bi
		n = self
		str = ""
		while n > 0
			str += (n % 2).to_s
			n >>= 1
		end
		str.scan(/./).reverse.join
	end
	def palindromic?
		self.to_s == self.to_s.scan(/./).reverse.join
	end
end

max = 1000000
puts (1...max).inject(0){|s, n|
	s + (n.palindromic? && n.to_bi.to_i.palindromic? ? n : 0)
}

解答

872187

感想

回文数の判断は文字列を反転させると簡単だった
特に難しくはない