Java ProgramsNumbersCheck Happy Number

Check Happy Number in Java

intermediate·  Numbers  ·  Number Theory

Problem

A happy number is one where repeatedly replacing it with the sum of the squares of its digits eventually reaches 1 — a number that isn't happy falls into an infinite repeating cycle instead.

Given a number, determine whether it is a happy number.

Input
19
Output
19 is a happy number: true

Java Program

Java
import java.util.HashSet; import java.util.Set; public class HappyNumberCheck { static int digitSquareSum(int num) { int sum = 0; while (num > 0) { int digit = num % 10; sum += digit * digit; num /= 10; } return sum; } public static void main(String[] args) { int num = 19; int original = num; Set<Integer> seen = new HashSet<>(); while (num != 1 && !seen.contains(num)) { seen.add(num); // remember this value so a repeat can be detected num = digitSquareSum(num); } System.out.println(original + " is a happy number: " + (num == 1)); } }

Output

19 is a happy number: true

Core Logic

Repeating the digit-square-sum transformation while tracking every value already seen catches an infinite cycle the moment it would repeat, instead of looping forever.

How It Works
  1. 1A HashSet<Integer> named seen records every value the number has already passed through.
  2. 2The loop continues as long as num isn't 1 and hasn't been seen before.
  3. 3Each pass adds the current num to seen, then computes the sum of the squares of its digits to get the next value.
  4. 4The loop ends either because num reached 1 (happy) or because it repeated a value already in seen (stuck in a cycle, not happy).
For 19: 19 → 82 → 68 → 100 → 1 — reaching 1 after four steps, so it's reported as happy.
💡

Key Point: Without the seen set, an unhappy number like 4 would loop forever — 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 repeats the cycle indefinitely, and the set is what recognizes 4 reappearing as a sign to stop.

Complexity
Time Complexity: O(log n)Space Complexity: O(log n)

Why: Every unhappy number falls into the same known 8-number cycle, so the set only ever needs to grow large enough to detect that repeat, bounded by how many digit-square-sum steps it takes to get there.

Key Concepts

HashSetcycle detectiondigit squares

Approach 2: Floyd's Cycle Detection

Java
public class HappyNumberFloyd { static int digitSquareSum(int num) { int sum = 0; while (num > 0) { int digit = num % 10; sum += digit * digit; num /= 10; } return sum; } public static void main(String[] args) { int num = 19; int slow = num, fast = digitSquareSum(num); // fast advances two steps for every one step slow takes while (fast != 1 && slow != fast) { slow = digitSquareSum(slow); fast = digitSquareSum(digitSquareSum(fast)); } System.out.println(num + " is a happy number: " + (fast == 1)); } }

Output

19 is a happy number: true

Core Logic

Two pointers moving through the same digit-square-sum sequence at different speeds — one step at a time, and two steps at a time — are guaranteed to meet if the sequence cycles, without needing to remember every value seen.

How It Works
  1. 1slow advances one digit-square-sum step at a time; fast advances two steps at a time.
  2. 2If the sequence reaches 1, either pointer landing on 1 ends the loop with a happy result.
  3. 3If the sequence instead cycles, fast — moving twice as fast — is guaranteed to lap back around and land on the same value as slow at some point.
  4. 4slow == fast (once both are past their starting value) confirms a cycle, meaning the number is not happy.
For 19, both pointers reach 1 before ever meeting elsewhere, confirming a happy number without ever storing a set of past values.
💡

Key Point: This is the same tortoise-and-hare technique used to detect cycles in a linked list, applied here to the sequence of digit-square sums instead of node pointers.

Complexity
Time Complexity: O(log n)Space Complexity: O(1)

Why: Only two running values — slow and fast — are ever kept, instead of a set that grows with every step of the sequence.

Key Concepts

tortoise and harecycle detection without extra memory

Related Programs