Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created May 29, 2011 18:04
Show Gist options
  • Save johana-star/997996 to your computer and use it in GitHub Desktop.
Save johana-star/997996 to your computer and use it in GitHub Desktop.
Euler Project Problem #036
# Solution to Project Euler's thirty-sixth problem
# http://projecteuler.net/index.php?section=problems&id=17
# Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
def test_decimal_palindromicity(number)
bool = number.to_s == number.to_s.reverse
return bool
end
def test_binary_palindromicity(number)
bool = number.to_s(2) == number.to_s(2).reverse
return bool
end
ceiling = 1000000
palindromes = Array.new
(1..ceiling).each do |number|
if number.odd? then # palindromic binary numbers are odd, testing evens is a performance lag.
if test_decimal_palindromicity(number) then # decimals are tested before binaries, as decimals have a lower incident of palindromicity.
if test_binary_palindromicity(number) then palindromes.push number end
end
end
end
p palindromes.reduce(:+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment