-
Notifications
You must be signed in to change notification settings - Fork 0
/
happynumber.java
28 lines (22 loc) · 917 Bytes
/
happynumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public boolean isHappy(int n) {
// Create a set to keep track of the numbers that have already been seen
Set<Integer> seen = new HashSet<>();
// Loop until either n becomes 1 (in which case n is a happy number) or n is seen again (in which case n is not a happy number)
while (n != 1 && !seen.contains(n)) {
// Add n to the set of seen numbers
seen.add(n);
// Compute the sum of the squares of the digits of n
int sum = 0;
while (n != 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
// Update n with the sum of the squares of its digits
n = sum;
}
// If n is 1, it is a happy number; otherwise, it is not
return n == 1;
}
}