Created
April 2, 2020 13:38
-
-
Save mAlishera/560470d32b85d5c07ea6e4304f9afc4b to your computer and use it in GitHub Desktop.
LC 30 days challenge - DAY2
This file contains 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
# 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