Java ProgramsNumbersCheck Magic Number

Check Magic Number in Java

beginner·  Numbers  ·  Number Theory

Problem

A magic number is one whose digits, when repeatedly summed down to a single digit, eventually reduce to exactly 1.

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

Input
40213
Output
40213 is a magic number: true

Java Program

Java
public class MagicNumberCheck { public static void main(String[] args) { int n = 40213; int x = n; while (x >= 10) { int sum = 0; while (x > 0) { sum += x % 10; // peel off the last digit x /= 10; } x = sum; // collapse to the digit sum, then check again } System.out.println(n + " is a magic number: " + (x == 1)); } }

Output

40213 is a magic number: true

Core Logic

Repeatedly collapsing the number down to the sum of its digits, until only one digit is left, mirrors the definition directly — check whether that final digit is 1.

How It Works
  1. 1The outer while (x >= 10) loop keeps collapsing x as long as it still has more than one digit.
  2. 2Inside it, an inner loop peels off digits one at a time with x % 10 and x /= 10, adding each into sum.
  3. 3Once every digit of the current x has been consumed, x is replaced by that digit sum.
  4. 4The outer loop repeats this collapse until x itself is a single digit, and the final check is x == 1.
For 40213, the digits sum to 4+0+2+1+3=10, and 10 then collapses to 1+0=1 — a single digit of 1, so it's reported as magic.
💡

Key Point: This single-digit result is called the digital root — a number's digital root is always between 1 and 9 (never 0, for a positive number), no matter how large it starts out.

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

Why: Each pass sums the digits of the current value, and the value shrinks fast enough that only a couple of passes are ever needed regardless of how large n starts.

Key Concepts

digit sumwhile loopdigital root

Approach 2: Digital Root Shortcut

Java
public class MagicNumberShortcut { public static void main(String[] args) { int n = 40213; // The digital root of n is 1 + (n - 1) % 9, so magic numbers are exactly n % 9 == 1 boolean isMagic = n % 9 == 1; System.out.println(n + " is a magic number: " + isMagic); } }

Output

40213 is a magic number: true

Core Logic

A number's digital root already has a closed-form formula — 1 + (n - 1) mod 9 — so the whole repeated-summing loop can collapse into a single modulo check.

How It Works
  1. 1The digital root formula simplifies to exactly this: a number is magic precisely when n % 9 == 1.
  2. 2n % 9 checks the remainder of n divided by 9 directly, without ever touching individual digits.
  3. 3If that remainder is 1, the number's digital root must be 1, and it's magic.
40213 % 9 evaluates to 1, matching the digital root of 1 found by manually summing digits.
💡

Key Point: This works because summing a number's digits never changes its remainder mod 9 — the repeated-digit-sum loop and this one-line check are mathematically the same operation, just expressed differently.

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

Why: The modulo trick replaces repeated digit summation with a single arithmetic operation, since the digital root of any number follows a fixed formula based on n mod 9.

Key Concepts

modulo arithmeticdigital root formula

Related Programs