In [9]:
def is_palindromes?(n)
n.to_s == n.to_s.reverse
end
In [10]:
is_palindromes?(101)
Out[10]:
In [11]:
def is_palindromes_by_double?(n)
n.to_s(2) == n.to_s(2).reverse
end
In [12]:
is_palindromes_by_double?(585)
Out[12]:
In [13]:
results = []
(1..1_000_000).each do |n|
results << n if is_palindromes?(n) and is_palindromes_by_double?(n)
end
results
Out[13]:
In [14]:
results.inject *:+
Out[14]:
In [ ]: