Created
May 17, 2015 15:11
-
-
Save emad-elsaid/81dc0b376de0b57916f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @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