Skip to content

Instantly share code, notes, and snippets.

@mAlishera
Created April 2, 2020 13:38
Show Gist options
  • Save mAlishera/560470d32b85d5c07ea6e4304f9afc4b to your computer and use it in GitHub Desktop.
Save mAlishera/560470d32b85d5c07ea6e4304f9afc4b to your computer and use it in GitHub Desktop.
LC 30 days challenge - DAY2
# https://leetcode.com/problems/happy-number/
def is_happy(n)
repeats = []
loop do
n = n.to_s.split('').inject(0) { |sum,x| sum + x.to_i*x.to_i }
return true if n == 1
return false if repeats.include?(n)
repeats << n
end
end
p is_happy(19)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment