Created
May 29, 2011 18:04
-
-
Save johana-star/997996 to your computer and use it in GitHub Desktop.
Euler Project Problem #036
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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