re: Write a script to find "Happy Numbers" VIEW POST

VIEW FULL DISCUSSION
 

Here is a ruby implementation.

def is_happy?(number)
  tmp = []

  while (number > 1) && (!tmp.include?(number))
    tmp << number
    # with ruby >=2.4 you can use number.digits
    digits = number.to_s.chars.map(&:to_i)
    number = digits.inject(0) { |total, value| total += value ** 2 }
  end

  number == 1
end
code of conduct - report abuse