AlgoMaster Logo

Strong Number Check

Last Updated: June 7, 2026

easy
5 min read

Understanding the Problem

A strong number equals the sum of the factorials of its own digits. Take 145: the digits are 1, 4, and 5, their factorials are 1, 24, and 120, and 1 + 24 + 120 is 145, so the number reproduces itself. The complete list of strong numbers is short: 1, 2, 145, and 40585. Every other positive integer fails the check.

A digit can only be one of ten values, 0 through 9, so the factorials you ever need are exactly 0! through 9!. A ten-entry lookup table covers every digit and is read in constant time, with no reason to recompute 5! each time the digit 5 appears.

Two boundary cases need attention. First, 0! is 1, not 0, so a digit of 0 contributes 1. The number 100 shows why this matters: its digits give 1! + 0! + 0! = 1 + 1 + 1 = 3, not 100, so 100 is not strong. Second, the definition applies only to positive integers, so 0 or any negative number returns false before any digit work begins.

Key Constraints:

  • n can be 0 or negative → These are not strong numbers by definition. Return false immediately, since the factorial-sum idea only applies to positive integers.
  • A digit of 0 contributes 0! = 1 → The factorial of zero is one, not zero. Counting it as zero would undercount the sum for numbers containing the digit 0, such as 100 or 40585.
  • Digits range only from 0 to 9 → Exactly ten factorials are ever needed, so a fixed ten-entry table answers every digit lookup in constant time.

Approach 1: Digit Factorials with a Lookup Table

Intuition

Compute the ten factorials 0! through 9! once into a small array, which turns each digit's factorial into a single array read.

With the table in place, the rest is a digit walk. n % 10 gives the last digit and n / 10 removes it, repeated until n reaches 0. For each digit, add its factorial from the table to a running sum, then compare the sum to the original number.

The original value has to be saved before the walk begins, because the loop divides n down to 0. The final comparison needs that saved copy.

Algorithm

  1. If n is less than or equal to 0, return false.
  2. Precompute factorials of 0 through 9 into a table where fact[d] holds d!. Note that fact[0] is 1.
  3. Save the original value of n.
  4. Set a running sum to 0.
  5. While n is greater than 0, take the last digit with n % 10, add fact[digit] to the sum, then remove the digit with n / 10.
  6. Return true if the sum equals the saved original value, otherwise false.

Example Walkthrough

Input:

145
n

The value 145 is positive, so the digit walk runs. The original 145 is saved for the final comparison. The first digit pulled off is 145 % 10 = 5, and fact[5] is 120, so the sum becomes 120. Dividing gives 14. The next digit is 14 % 10 = 4, and fact[4] is 24, so the sum becomes 144. Dividing gives 1. The last digit is 1 % 10 = 1, and fact[1] is 1, so the sum becomes 145. Dividing gives 0, which ends the loop. The sum 145 equals the saved original 145, so the result is true.

true
result

Code

Approach 2: Factorial Computed Per Digit

Intuition

This approach skips the table and computes each digit's factorial on the fly with a short loop. The code becomes self-contained, at the cost of redoing the factorial work for digits that repeat.

The digit walk is identical to the first approach. The difference is that for each digit, a helper multiplies 1 * 2 * ... * d to get d!, with the empty product for digit 0 yielding 1 so that 0! stays correct.

A digit's factorial loop runs at most nine multiplications and the outer walk visits a constant number of digits for a 32-bit integer, so this stays fast. The trade-off against the table is more arithmetic per digit but no separate data structure to set up.

Algorithm

  1. If n is less than or equal to 0, return false.
  2. Save the original value of n.
  3. Set a running sum to 0.
  4. While n is greater than 0, read the last digit with n % 10, compute its factorial by multiplying 1 through that digit, add the factorial to the sum, then remove the digit with n / 10.
  5. Return true if the sum equals the saved original value, otherwise false.

Example Walkthrough

Input:

123
n

The value 123 is positive, so the walk runs, and 123 is saved. The first digit is 123 % 10 = 3, whose factorial is 3! = 6, so the sum becomes 6. Dividing gives 12. The next digit is 12 % 10 = 2, whose factorial is 2! = 2, so the sum becomes 8. Dividing gives 1. The last digit is 1 % 10 = 1, whose factorial is 1! = 1, so the sum becomes 9. Dividing gives 0, ending the loop. The sum 9 does not equal the saved original 123, so the result is false.

false
result

Code