Skip to content
Merged
Show file tree
Hide file tree
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
57 changes: 57 additions & 0 deletions src/main/java/com/thealgorithms/maths/HappyNumber.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 0 additions & 39 deletions src/main/java/com/thealgorithms/others/HappyNumbersSeq.java

This file was deleted.

32 changes: 32 additions & 0 deletions src/test/java/com/thealgorithms/maths/HappyNumberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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
}
}