Skip to content

Instantly share code, notes, and snippets.

@lukad
Created January 2, 2012 13:13
Show Gist options
  • Save lukad/1550642 to your computer and use it in GitHub Desktop.
Save lukad/1550642 to your computer and use it in GitHub Desktop.
Armstrong numbers
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
@lukad
Copy link
Author

lukad commented Jan 2, 2012

It probably isn't the fastest solution but a pretty one in my (noobish) opinion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment