From 63a08ba381a8380ccbfea2b2f9b7d172f9925df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=5Bgithub=5D=E2=80=9D?= Date: Wed, 1 Oct 2025 18:51:37 +0530 Subject: [PATCH 1/5] Added HappyNumber algorithm in maths section --- .../com/thealgorithms/maths/HappyNumber.java | 57 +++++++++++++++++++ .../thealgorithms/maths/HappyNumberTest.java | 31 ++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/HappyNumber.java create mode 100644 src/test/java/com/thealgorithms/maths/HappyNumberTest.java diff --git a/src/main/java/com/thealgorithms/maths/HappyNumber.java b/src/main/java/com/thealgorithms/maths/HappyNumber.java new file mode 100644 index 000000000000..b1dd73463400 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/HappyNumber.java @@ -0,0 +1,57 @@ +package com.thealgorithms.maths; + +/** + * A Happy Number is defined as a number which eventually reaches 1 when replaced + * by the sum of the squares of each digit. + * If it falls into a cycle that does not include 1, then it is not a happy number. + * + * Example: + * 19 → 1² + 9² = 82 + * 82 → 8² + 2² = 68 + * 68 → 6² + 8² = 100 + * 100 → 1² + 0² + 0² = 1 → Happy Number! + */ +public final class HappyNumber { + + private HappyNumber() { + } + + /** + * Checks whether the given number is a Happy Number. + * Uses Floyd’s Cycle Detection algorithm (tortoise and hare method) + * to detect loops efficiently. + * + * @param n The number to check + * @return true if n is a Happy Number, false otherwise + */ + public static boolean isHappy(int n) { + int slow = n; + int fast = n; + + do { + slow = sumOfSquares(slow); // move 1 step + fast = sumOfSquares(sumOfSquares(fast)); // move 2 steps + } while (slow != fast); + + return slow == 1; // If cycle ends in 1 → Happy number + } + + /** + * Calculates the sum of squares of the digits of a number. + * + * Example: + * num = 82 → 8² + 2² = 64 + 4 = 68 + * + * @param num The number to calculate sum of squares of digits + * @return The sum of squares of the digits + */ + private static int sumOfSquares(int num) { + int sum = 0; + while (num > 0) { + int digit = num % 10; + sum += digit * digit; + num /= 10; + } + return sum; + } +} diff --git a/src/test/java/com/thealgorithms/maths/HappyNumberTest.java b/src/test/java/com/thealgorithms/maths/HappyNumberTest.java new file mode 100644 index 000000000000..c086b3721dd0 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/HappyNumberTest.java @@ -0,0 +1,31 @@ +package com.thealgorithms.maths; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +public class HappyNumberTest { + + @Test + void testHappyNumbers() { + // Known happy numbers + assertTrue(HappyNumber.isHappy(1)); + assertTrue(HappyNumber.isHappy(7)); + assertTrue(HappyNumber.isHappy(19)); + assertTrue(HappyNumber.isHappy(100)); + } + + @Test + void testUnhappyNumbers() { + // Known unhappy numbers + assertFalse(HappyNumber.isHappy(2)); + assertFalse(HappyNumber.isHappy(4)); + assertFalse(HappyNumber.isHappy(20)); + } + + @Test + void testLargeNumber() { + // Just to check behavior with larger input + assertTrue(HappyNumber.isHappy(1000000)); // reduces to 1 eventually + } +} From 194c699e876d93917edf22763fe38ca43290103d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=5Bgithub=5D=E2=80=9D?= Date: Wed, 1 Oct 2025 19:22:55 +0530 Subject: [PATCH 2/5] Fix Checkstyle: remove trailing spaces in HappyNumber --- src/main/java/com/thealgorithms/maths/HappyNumber.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/HappyNumber.java b/src/main/java/com/thealgorithms/maths/HappyNumber.java index b1dd73463400..9e1dba797273 100644 --- a/src/main/java/com/thealgorithms/maths/HappyNumber.java +++ b/src/main/java/com/thealgorithms/maths/HappyNumber.java @@ -1,8 +1,8 @@ package com.thealgorithms.maths; /** - * A Happy Number is defined as a number which eventually reaches 1 when replaced - * by the sum of the squares of each digit. + * A Happy Number is defined as a number which eventually reaches 1 when replaced + * by the sum of the squares of each digit. * If it falls into a cycle that does not include 1, then it is not a happy number. * * Example: @@ -18,7 +18,7 @@ private HappyNumber() { /** * Checks whether the given number is a Happy Number. - * Uses Floyd’s Cycle Detection algorithm (tortoise and hare method) + * Uses Floyd’s Cycle Detection algorithm (tortoise and hare method) * to detect loops efficiently. * * @param n The number to check From f8c62882ab231200008bf7fe4f594149e0aa3cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=5Bgithub=5D=E2=80=9D?= Date: Wed, 1 Oct 2025 19:36:13 +0530 Subject: [PATCH 3/5] Fix formatting: remove trailing spaces and apply clang-format --- .../com/thealgorithms/maths/HappyNumber.java | 16 ++++++++-------- .../com/thealgorithms/maths/HappyNumberTest.java | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/HappyNumber.java b/src/main/java/com/thealgorithms/maths/HappyNumber.java index 9e1dba797273..bfe746953b33 100644 --- a/src/main/java/com/thealgorithms/maths/HappyNumber.java +++ b/src/main/java/com/thealgorithms/maths/HappyNumber.java @@ -4,12 +4,12 @@ * A Happy Number is defined as a number which eventually reaches 1 when replaced * by the sum of the squares of each digit. * If it falls into a cycle that does not include 1, then it is not a happy number. - * + * Example: - * 19 → 1² + 9² = 82 - * 82 → 8² + 2² = 68 - * 68 → 6² + 8² = 100 - * 100 → 1² + 0² + 0² = 1 → Happy Number! + * 19 → 1² + 9² = 82 + * 82 → 8² + 2² = 68 + * 68 → 6² + 8² = 100 + * 100 → 1² + 0² + 0² = 1 → Happy Number! */ public final class HappyNumber { @@ -29,8 +29,8 @@ public static boolean isHappy(int n) { int fast = n; do { - slow = sumOfSquares(slow); // move 1 step - fast = sumOfSquares(sumOfSquares(fast)); // move 2 steps + slow = sumOfSquares(slow); // move 1 step + fast = sumOfSquares(sumOfSquares(fast)); // move 2 steps } while (slow != fast); return slow == 1; // If cycle ends in 1 → Happy number @@ -40,7 +40,7 @@ public static boolean isHappy(int n) { * Calculates the sum of squares of the digits of a number. * * Example: - * num = 82 → 8² + 2² = 64 + 4 = 68 + * num = 82 → 8² + 2² = 64 + 4 = 68 * * @param num The number to calculate sum of squares of digits * @return The sum of squares of the digits diff --git a/src/test/java/com/thealgorithms/maths/HappyNumberTest.java b/src/test/java/com/thealgorithms/maths/HappyNumberTest.java index c086b3721dd0..ff7f41be6298 100644 --- a/src/test/java/com/thealgorithms/maths/HappyNumberTest.java +++ b/src/test/java/com/thealgorithms/maths/HappyNumberTest.java @@ -26,6 +26,6 @@ void testUnhappyNumbers() { @Test void testLargeNumber() { // Just to check behavior with larger input - assertTrue(HappyNumber.isHappy(1000000)); // reduces to 1 eventually + assertTrue(HappyNumber.isHappy(1000000)); // reduces to 1 eventually } } From 16204bef75b774847c88f4c5dc9977b5f9f8283b Mon Sep 17 00:00:00 2001 From: Ruturaj Jadhav Date: Wed, 1 Oct 2025 19:45:55 +0530 Subject: [PATCH 4/5] Update HappyNumberTest.java --- src/test/java/com/thealgorithms/maths/HappyNumberTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/maths/HappyNumberTest.java b/src/test/java/com/thealgorithms/maths/HappyNumberTest.java index ff7f41be6298..4b7cb795406f 100644 --- a/src/test/java/com/thealgorithms/maths/HappyNumberTest.java +++ b/src/test/java/com/thealgorithms/maths/HappyNumberTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; + import org.junit.jupiter.api.Test; public class HappyNumberTest { From 13d7f19d9476e4de2d49ad056840491ef6b32c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=5Bgithub=5D=E2=80=9D?= Date: Thu, 9 Oct 2025 09:17:58 +0530 Subject: [PATCH 5/5] Removed old HappyNumbersSeq.java as replaced by new HappyNumber.java --- .../thealgorithms/others/HappyNumbersSeq.java | 39 ------------------- 1 file changed, 39 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/others/HappyNumbersSeq.java diff --git a/src/main/java/com/thealgorithms/others/HappyNumbersSeq.java b/src/main/java/com/thealgorithms/others/HappyNumbersSeq.java deleted file mode 100644 index 0ae1e451bc6a..000000000000 --- a/src/main/java/com/thealgorithms/others/HappyNumbersSeq.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.thealgorithms.others; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Scanner; -import java.util.Set; - -public final class HappyNumbersSeq { - private HappyNumbersSeq() { - } - - private static final Set CYCLE_NUMS = new HashSet<>(Arrays.asList(4, 16, 20, 37, 58, 145)); - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - System.out.print("Enter number: "); - int n = in.nextInt(); - while (n != 1 && !isSad(n)) { - System.out.print(n + " "); - n = sumSquares(n); - } - String res = n == 1 ? "1 Happy number" : "Sad number"; - System.out.println(res); - in.close(); - } - - private static int sumSquares(int n) { - int s = 0; - for (; n > 0; n /= 10) { - int r = n % 10; - s += r * r; - } - return s; - } - - private static boolean isSad(int n) { - return CYCLE_NUMS.contains(n); - } -}