Skip to content

Commit 3930fe2

Browse files
authored
Merge pull request neetcode-gh#1903 from saip7795/sp/happy-number
Create: 0202-happy-number.rb
2 parents 2715a29 + 0768bd6 commit 3930fe2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: ruby/0202-happy-number.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def is_happy(n)
2+
visit = Set.new([])
3+
4+
while !visit.include?(n)
5+
visit.add(n)
6+
n = sum_of_squares(n)
7+
return true if n==1
8+
end
9+
10+
return false
11+
12+
end
13+
14+
def sum_of_squares(n)
15+
output = 0
16+
while n != 0
17+
digit = n%10
18+
output += (digit*digit)
19+
n = n/10
20+
end
21+
return output
22+
end

0 commit comments

Comments
 (0)