Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions 47 Miscellaneous/SOE/HappyNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package SOE;

import java.util.HashSet;
import java.util.Set;

public class HappyNumber {

// 計算各位數平方和
static int next(int n) {
int sum = 0;
while (n > 0) {
int d = n % 10;
sum += d * d;
n /= 10;
}
return sum;
}

// 遞迴版 isHappy()
static boolean isHappy(int n) {
return isHappyHelper(n, new HashSet<>());
}

// 遞迴輔助函式:傳遞 seen 集合
private static boolean isHappyHelper(int n, Set<Integer> seen) {
if (n == 1)
return true; // 終止條件:快樂數
if (seen.contains(n))
return false; // 終止條件:進入循環

seen.add(n);
return isHappyHelper(next(n), seen);
}

// 測試
public static void main(String[] args) {
int[] nums = {1, 7, 19, 20, 25, 4};

for (int n : nums) {
System.out.printf("%d -> %s%n", n, isHappy(n));
}
}
}

// Source: https://chatgpt.com/c/69104fa3-4828-8321-ade1-f983f1f8c14c


Morty Proxy This is a proxified and sanitized view of the page, visit original site.