Created
January 2, 2012 13:13
-
-
Save lukad/1550642 to your computer and use it in GitHub Desktop.
Armstrong numbers
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 Integer | |
def to_a | |
n = [] | |
x = self | |
while x > 0 | |
n << x.modulo(10) | |
x -= n.last | |
x /= 10 | |
end | |
n.reverse | |
end | |
def armstrong? | |
self == self.to_a.map { |x| x**self.to_s.length }.inject(:+) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It probably isn't the fastest solution but a pretty one in my (noobish) opinion.