Skip to content

Instantly share code, notes, and snippets.

@ysnerdem
Created March 15, 2018 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysnerdem/6b370a7da3b5bbe5e61d9d86c8782e6b to your computer and use it in GitHub Desktop.
Save ysnerdem/6b370a7da3b5bbe5e61d9d86c8782e6b to your computer and use it in GitHub Desktop.
Armstrong numbers
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