Introduction

A strong number is a classic Java programming problem that combines two fundamental concepts you've likely already learned: extracting digits from a number and calculating factorials. Once you understand these two building blocks, checking whether a number is strong becomes a straightforward combination of both.

Because this problem tests loops, functions, recursion, and basic optimization techniques, it frequently appears in Java programming exercises and coding interviews.

In this guide, you'll learn what a strong number is, implement it using a while loop, recursion, and an optimized precomputed factorial approach, print strong numbers within a range, and understand the performance improvements that make the optimized solution faster.

Advertisement

What Is a Strong Number?

A strong number is a number whose value is equal to the sum of the factorials of its individual digits.

For example:

145

Its digits are:

1
4
5

Their factorials are:

1! = 1
4! = 24
5! = 120

Adding them:

1 + 24 + 120 = 145

Since the sum equals the original number, 145 is a strong number.

Another well-known strong number is:

40585

Method 1: Using a While Loop with a Helper Factorial Function

This is the standard and most commonly taught solution.

Java Program

public class StrongNumberCheck {

    static int factorial(int n) {

        int fact = 1;

        for (int i = 1; i <= n; i++) {
            fact *= i;
        }

        return fact;
    }

    public static void main(String[] args) {

        int num = 145;
        int original = num;
        int sum = 0;

        while (num != 0) {

            int digit = num % 10;

            sum += factorial(digit);

            num /= 10;
        }

        if (sum == original) {
            System.out.println(original + " is a strong number.");
        } else {
            System.out.println(original + " is not a strong number.");
        }
    }
}

Output

145 is a strong number.

Step-by-Step Trace (num = 145)

Iteration num Digit Factorial Running Sum
1 145 5 120 120
2 14 4 24 144
3 1 1 1 145

Since:

sum = 145

which matches the original number, the program correctly identifies it as a strong number.

Why Use a Separate factorial() Method?

Breaking the problem into two smaller tasks makes the code easier to understand:

  • Extract one digit.

  • Calculate its factorial.

  • Add it to the running sum.

This modular approach also allows the factorial method to be reused elsewhere.


Method 2: Using Recursion

Both the factorial calculation and digit processing can be written recursively.

Java Program

public class StrongNumberRecursion {

    static int factorial(int n) {

        if (n == 0 || n == 1) {
            return 1;
        }

        return n * factorial(n - 1);
    }

    static int sumOfFactorials(int num) {

        if (num == 0) {
            return 0;
        }

        int digit = num % 10;

        return factorial(digit) + sumOfFactorials(num / 10);
    }

    public static void main(String[] args) {

        int num = 145;

        if (sumOfFactorials(num) == num) {
            System.out.println(num + " is a strong number.");
        } else {
            System.out.println(num + " is not a strong number.");
        }
    }
}

Output

145 is a strong number.

How It Works

Two recursive functions work together:

  • sumOfFactorials() processes one digit at a time.

  • factorial() computes the factorial of each extracted digit.

This is an example of nested recursion, where one recursive function calls another recursive function.


Method 3: Printing Strong Numbers in a Range

A common interview variation asks you to print every strong number within a given range.

Java Program

public class StrongNumbersInRange {

    static int factorial(int n) {

        int fact = 1;

        for (int i = 1; i <= n; i++) {
            fact *= i;
        }

        return fact;
    }

    static boolean isStrong(int num) {

        int temp = num;
        int sum = 0;

        while (temp != 0) {

            int digit = temp % 10;

            sum += factorial(digit);

            temp /= 10;
        }

        return sum == num;
    }

    public static void main(String[] args) {

        int start = 1;
        int end = 1000;

        System.out.println("Strong numbers:");

        for (int num = start; num <= end; num++) {

            if (isStrong(num)) {
                System.out.print(num + " ");
            }
        }
    }
}

Output

Strong numbers:
1 2 145

Strong numbers are extremely rare.

Within the first 1,000 numbers, only:

  • 1

  • 2

  • 145

are strong numbers.


Method 4: Using Precomputed Factorials (Optimized)

Digits can only be:

0 to 9

Therefore, there are only 10 factorial values that ever need to be calculated.

Instead of repeatedly computing factorials, calculate them once and store them in an array.

Java Program

public class StrongNumberPrecomputed {

    static int[] factorial = new int[10];

    static void precomputeFactorials() {

        factorial[0] = 1;

        for (int i = 1; i < 10; i++) {
            factorial[i] = factorial[i - 1] * i;
        }
    }

    static boolean isStrong(int num) {

        int temp = num;
        int sum = 0;

        while (temp != 0) {

            int digit = temp % 10;

            sum += factorial[digit];

            temp /= 10;
        }

        return sum == num;
    }

    public static void main(String[] args) {

        precomputeFactorials();

        int num = 145;

        if (isStrong(num)) {
            System.out.println(num + " is a strong number.");
        } else {
            System.out.println(num + " is not a strong number.");
        }
    }
}

Output

145 is a strong number.

Why Is This Faster?

Without precomputation:

Every digit requires another factorial calculation.

Example:

145

requires computing:

5!
4!
1!

every time it is checked.

With precomputation:

Factorials are already stored.

Instead of calculating:

factorial(5)

the program simply performs:

factorial[5]

Array lookup is much faster than repeated computation, especially when checking thousands of numbers.


How Java Handles This Internally

Methods 1–3

The following variables are primitive values stored on the stack:

  • num

  • sum

  • digit

  • temp

Each call to factorial() creates a small stack frame.


Method 4

The factorial array:

int[] factorial

is stored on the heap.

Since it contains only 10 elements, the memory cost is extremely small.

After initialization, every factorial is retrieved in constant time.


Real-Life Analogy

Imagine a company where every employee contributes based on the complexity of their work.

Instead of adding their normal effort, you apply a special multiplier (their factorial).

If the combined weighted contribution of every employee equals the company's total output exactly, you have a perfectly balanced organization.

A strong number works in exactly the same way.

Each digit contributes its factorial, and the total recreates the original number.


Comparison Table

Method Recalculates Factorials? Best Used When
While Loop + Helper Method Yes Single number checks
Recursion Yes Learning recursion
Precomputed Factorials No Checking many numbers efficiently

Best Practices

  • Use a separate factorial() helper method for readability.

  • Preserve the original number before extracting digits.

  • Precompute factorials when checking many numbers.

  • Store the factorial values for digits 0–9 only once.

  • Reuse the same isStrong() method throughout your program.


Common Mistakes

Forgetting the Original Number

Always preserve:

int original = num;

Otherwise, after digit extraction:

num = 0

making the final comparison incorrect.


Recomputing Factorials Repeatedly

Avoid repeatedly calling:

factorial(digit)

inside large loops.

Instead, precompute all ten factorials once.


Confusing Strong Numbers with Armstrong Numbers

Strong numbers use:

sum of factorials of digits

Armstrong numbers use:

sum of powers of digits

These are completely different concepts.


Incorrect Factorial Logic

The factorial loop should start from:

1

not:

0

Starting from zero makes every factorial equal zero.


Assuming Strong Numbers Are Common

Strong numbers are extremely rare.

Finding only a few within thousands of numbers is completely normal.


Expert Tips

A strong interview answer is:

"A strong number is one where the sum of the factorials of its digits equals the original number. I extract each digit using modulus and division, calculate its factorial using a helper function, and accumulate the total. For checking many numbers, I optimize the solution by precomputing the factorials of digits 0 through 9 once in an array, replacing repeated factorial calculations with constant-time array lookups."

Mentioning the precomputed factorial optimization demonstrates an understanding of algorithm optimization beyond simply solving the problem.


Pros and Cons

Method Advantages Disadvantages
While Loop + Helper Simple, modular, easy to understand Recalculates factorials repeatedly
Recursion Demonstrates recursive thinking More stack overhead
Precomputed Factorials Fastest for multiple checks Slightly more setup code

Frequently Asked Questions

What is a strong number?

A number whose value equals the sum of the factorials of its digits.

Example:

145

1! + 4! + 5! = 145

What is the difference between a strong number and an Armstrong number?

A strong number uses factorials of digits.

An Armstrong number uses powers of digits.


What are some well-known strong numbers?

Some commonly known strong numbers are:

1
2
145
40585

Why should I precompute factorials?

Digits only range from 0 to 9.

Instead of calculating factorials repeatedly, compute them once and reuse them through array lookups.


Are all single-digit numbers strong numbers?

No.

Only:

1
2

are strong numbers because:

1! = 1
2! = 2

Digits such as 3, 4, and 5 are not strong numbers because:

3! = 6
4! = 24
5! = 120

which are not equal to the original digits.


Can I solve this using recursion?

Yes.

Both the digit processing and factorial calculation can be implemented recursively.


What is the time complexity?

Using repeated factorial calculations:

O(d × k)

where:

  • d = number of digits

  • k = cost of computing each factorial

With precomputed factorials:

O(d)

since each factorial lookup is constant time.


How do I print all strong numbers within a range?

Loop through every number in the range and call the reusable isStrong() method.


Why are strong numbers so rare?

Factorials grow extremely quickly.

As numbers become larger, the sum of digit factorials almost never equals the original number.


Is this a common interview question?

Yes.

It is frequently used to test whether candidates can combine digit extraction with factorial computation and discuss simple optimization techniques.


What data type should I use?

int is sufficient for the common strong number problems because the largest digit factorial is:

9! = 362880

which comfortably fits within the int range.


Is a strong number the same as a SPY number?

No.

A strong number compares the number with the sum of the factorials of its digits.

A SPY number compares the sum of digits with the product of digits.

They are completely different mathematical concepts.