Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1903 from saip7795/sp/happy-number
Browse files Browse the repository at this point in the history
Create: 0202-happy-number.rb
  • Loading branch information
tahsintunan authored Jan 8, 2023
2 parents 2715a29 + 0768bd6 commit 3930fe2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ruby/0202-happy-number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def is_happy(n)
visit = Set.new([])

while !visit.include?(n)
visit.add(n)
n = sum_of_squares(n)
return true if n==1
end

return false

end

def sum_of_squares(n)
output = 0
while n != 0
digit = n%10
output += (digit*digit)
n = n/10
end
return output
end

0 comments on commit 3930fe2

Please sign in to comment.