Skip to content

Commit

Permalink
Ruby Solution for Happy Number
Browse files Browse the repository at this point in the history
  • Loading branch information
saip7795 committed Jan 5, 2023
1 parent eff3535 commit 0768bd6
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 0768bd6

Please sign in to comment.