読者です 読者をやめる 読者になる 読者になる

ARMSTRONG NUMBERS(CodeEval)

こういう数値のことをArmstrong Numbersっていうんですね。

CHALLENGE DESCRIPTION:

An Armstrong number is an n-digit number that is equal to the sum of the n'th powers of its digits. Determine if the input numbers are Armstrong numbers.

INPUT SAMPLE:

Your program should accept as its first argument a path to a filename. Each line in this file has a positive integer. E.g.

6
153
351

OUTPUT SAMPLE:

Print out True/False if the number is an Armstrong number or not. E.g.

True
True
False

MyCode

#!/usr/bin/env ruby -w

def armstrong_numbers(n)
  digit = n.size
  sum = 0
  n.each_char do |c|
    sum += c.to_i ** digit
  end
  (sum == n.to_i).to_s.capitalize
end

ARGF.each_line do |line|
  puts armstrong_numbers(line.chomp)
end