Skip to content

Instantly share code, notes, and snippets.

@emad-elsaid
Created May 17, 2015 15:11
Show Gist options
  • Save emad-elsaid/81dc0b376de0b57916f8 to your computer and use it in GitHub Desktop.
Save emad-elsaid/81dc0b376de0b57916f8 to your computer and use it in GitHub Desktop.
# @param {Integer} n
# @return {Boolean}
def is_happy(n)
iterated = []
found = false
loop do
found = iterated.include? n
iterated << n
nxt = 0
while n>0 do
digit = n % 10
nxt += digit * digit
n = (n / 10).floor
end
n = nxt
break if n==1 || found
end
n==1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment