File tree Expand file tree Collapse file tree 2 files changed +9
-9
lines changed
main/java/com/thealgorithms/maths
test/java/com/thealgorithms/maths Expand file tree Collapse file tree 2 files changed +9
-9
lines changed Original file line number Diff line number Diff line change 44 * A Happy Number is defined as a number which eventually reaches 1 when replaced
55 * by the sum of the squares of each digit.
66 * If it falls into a cycle that does not include 1, then it is not a happy number.
7- *
7+
88 * Example:
9- * 19 → 1² + 9² = 82
10- * 82 → 8² + 2² = 68
11- * 68 → 6² + 8² = 100
12- * 100 → 1² + 0² + 0² = 1 → Happy Number!
9+ * 19 → 1² + 9² = 82
10+ * 82 → 8² + 2² = 68
11+ * 68 → 6² + 8² = 100
12+ * 100 → 1² + 0² + 0² = 1 → Happy Number!
1313 */
1414public final class HappyNumber {
1515
@@ -29,8 +29,8 @@ public static boolean isHappy(int n) {
2929 int fast = n ;
3030
3131 do {
32- slow = sumOfSquares (slow ); // move 1 step
33- fast = sumOfSquares (sumOfSquares (fast )); // move 2 steps
32+ slow = sumOfSquares (slow ); // move 1 step
33+ fast = sumOfSquares (sumOfSquares (fast )); // move 2 steps
3434 } while (slow != fast );
3535
3636 return slow == 1 ; // If cycle ends in 1 → Happy number
@@ -40,7 +40,7 @@ public static boolean isHappy(int n) {
4040 * Calculates the sum of squares of the digits of a number.
4141 *
4242 * Example:
43- * num = 82 → 8² + 2² = 64 + 4 = 68
43+ * num = 82 → 8² + 2² = 64 + 4 = 68
4444 *
4545 * @param num The number to calculate sum of squares of digits
4646 * @return The sum of squares of the digits
Original file line number Diff line number Diff line change @@ -26,6 +26,6 @@ void testUnhappyNumbers() {
2626 @ Test
2727 void testLargeNumber () {
2828 // Just to check behavior with larger input
29- assertTrue (HappyNumber .isHappy (1000000 )); // reduces to 1 eventually
29+ assertTrue (HappyNumber .isHappy (1000000 )); // reduces to 1 eventually
3030 }
3131}
You can’t perform that action at this time.
0 commit comments