Created
March 12, 2014 03:33
-
-
Save tlowrimore/9500335 to your computer and use it in GitHub Desktop.
Detects whether a number is palindromic without converting the number to a String.
This file contains 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
module Palindrome | |
def self.palindrome?(n) | |
n == reverse(n) | |
end | |
def self.reverse(n) | |
rs = 0 | |
while n > 0 | |
n, r = n.divmod 10 | |
rs = rs * 10 + r | |
end | |
rs | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment