The decimal number, 585 = 10010010012 (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.
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.)
Solution (in Ruby)
The solution is one the simplest and straightforward when implemented in Ruby.
sum = 0 1.upto(1000000) do |num| sum+=num if num.to_s == num.to_s.reverse && num.to_s(base=2) == num.to_s(base=2).reverse end puts "Sum is #{sum}"
Hover here to see the solution
Cheers!!
Bragaadeesh
No comments:
Post a Comment