Created
December 29, 2013 01:52
-
-
Save syntacticsugar/8166585 to your computer and use it in GitHub Desktop.
project euler #36, first pass.
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
class String | |
def palindrome? | |
self == self.reverse | |
end | |
end | |
class Integer | |
def palindrome?(b=10) | |
self.to_s(b).palindrome? | |
end | |
end | |
(1...1000000).select { |x| x.palindrome? }.select { |x| x.palindrome?(2) }.inject(:+) | |
class Integer
def to_b
a, r = self.divmod 2
if a > 0
a.to_b << r.to_s
else
r.to_s
end
end
def to_x(base)
a, r = self.divmod base
if a > 0
a.to_b << r.to_s
else
r.to_s
end
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with self constructed Ruby method to convert to binary: