easyNumber ProblemsJava

Check Whether a Number Is a Magic Number

Decide whether a number is magic by reducing its repeated digit sum to a single digit of 1.

Quick Answer

A magic number is one whose repeated digit sum eventually becomes 1. Keep summing the digits of the result until a single digit is left, then check whether it equals 1. For 1729 the sums go 19, then 10, then 1, so 1729 is a magic number.

Problem Statement

A magic number is a positive integer for which the repeated sum of its digits eventually reduces to 1. You keep replacing the number with the sum of its digits until a single digit remains; if that digit is 1, the number is magic. Given n, return true or false.

For example, 1729 reduces as 1+7+2+9 = 19, then 1+9 = 10, then 1+0 = 1. The final single digit is 1, so 1729 is a magic number.

Input: A single positive integer n.

Output: A boolean: true if n is a magic number, otherwise false.

Examples

Example 1
Input:  1729
Output: true

1+7+2+9 = 19 -> 1+9 = 10 -> 1+0 = 1, a single digit of 1.

Example 2
Input:  12
Output: false

1+2 = 3, a single digit that is not 1.

Constraints

  • n >= 1 and fits in a 32-bit int
  • The reduction continues until a single digit remains

Think Before You Code

Reveal the questions to ask yourself first
  • When should the repeated summing stop?
  • How is this related to the digital root of a number?
  • Do you need a loop inside a loop, and what does each one do?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
You repeat the digit-sum step until the value drops below 10.
Hint 2
An outer loop drives the reduction; an inner loop computes one digit sum.
Hint 3
After the outer loop ends, the value is a single digit — return `value == 1`.

Approach

Reveal the step-by-step approach

Reduce the number down to its digital root.

  1. Work on a copy of n.
  2. While the copy is 10 or more:
    • compute the digit sum with an inner loop (sum += copy % 10; copy /= 10).
    • set the copy to that sum.
  3. When the copy is a single digit, return copy == 1.

The repeated digit sum is exactly the digital root, and a number is magic when that root is 1.

Dry Run

Walk through the example step by step

Checking n = 1729:

pass | value | digit sum
-----+-------+----------------------
1    | 1729  | 1+7+2+9 = 19
2    | 19    | 1+9      = 10
3    | 10    | 1+0      = 1
stop | 1     | single digit

value (1) == 1  ->  true

Solution

Reveal the full Java solution
public class MagicNumber {
    public static boolean isMagic(int n) {
        int value = Math.abs(n);
        while (value >= 10) {
            int sum = 0;
            while (value != 0) {
                sum += value % 10;
                value /= 10;
            }
            value = sum;
        }
        return value == 1;
    }

    public static void main(String[] args) {
        System.out.println(isMagic(1729)); // true
        System.out.println(isMagic(12));   // false
    }
}

The outer loop keeps collapsing the number while it still has more than one digit; the inner loop performs a single digit-sum pass. When the outer loop exits, value holds the digital root, and the number is magic exactly when that root is 1. This is why a shortcut using (n - 1) % 9 == 0 also works.

Time: O(d) per pass with very few passes (near O(log n) overall)Space: O(1)

Common Mistakes

  • Stopping after one digit-sum pass instead of reducing all the way to one digit.
  • Comparing the final digit to 0 instead of 1.

Edge Cases to Test

  • n = 1 is already a single digit equal to 1, so it is magic.
  • n = 10: 1+0 = 1, so it is magic even though it is two digits.

Interview Follow-Ups

  • How would you use the digital-root formula (n - 1) % 9 == 0 to answer in O(1)?
  • How does this differ from checking a general digital root value other than 1?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us