Created
May 6, 2013 14:48
-
-
Save sethetter/5525621 to your computer and use it in GitHub Desktop.
Armstrong Numbers
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
#!/usr/bin/env ruby | |
# Coding challenge : Armstrong Numbers | |
# | |
# Write a program using any language or technique that can accept two positive | |
# integers defining a range (such as 0 and 100 for the range 0 <= x <= 100) and | |
# will print all Armstrong Numbers in that range. | |
# | |
# An Armstrong Number is fully defined at | |
# http://en.wikipedia.org/wiki/Narcissistic_number | |
# | |
# In short it is any number where the value is the same as the sum of it's | |
# digits raised to the power of the number of it's digits. | |
# | |
# So 153 = 1^3 + 5^3 + 3^3 | |
# | |
# Given the range 0 to 10,000 you should have the following output: | |
# 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474 | |
# | |
# We are assuming base 10. | |
low = ARGV[0] | |
high = ARGV[1] | |
(low..high).each do |num| | |
digits = num.to_s.split('').to_a | |
num_digits = digits.length | |
result = 0 | |
digits.each do |digit| | |
result += (digit.to_i**num_digits) | |
end | |
puts num if result.to_i == num.to_i | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment