Created
March 15, 2018 21:51
-
-
Save ysnerdem/6b370a7da3b5bbe5e61d9d86c8782e6b 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
def is_armstrong(num) | |
num = num.to_s | |
sum = 0 | |
num.each_char do |i| | |
i = i.to_i | |
sum += (i ** 3) | |
end | |
num = num.to_i | |
if sum == num | |
puts "#{num} is armstrong number" | |
else | |
puts "#{num} is not armstrong number" | |
end | |
end | |
def find_armstrong_nums(range) | |
(1..range).each do |num| | |
num = num.to_s | |
sum = 0 | |
num.each_char do |i| | |
i = i.to_i | |
sum += (i ** 3) | |
num = num.to_i | |
end | |
if sum == num | |
puts num | |
end | |
end | |
end | |
is_armstrong(153) | |
find_armstrong_nums(1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment