Check Whether a Number Is a Strong Number
Determine whether a number equals the sum of the factorials of its digits.
A strong number equals the sum of the factorials of its digits. Extract each digit, add its factorial to a running total, and compare with the original number. For 145, 1! + 4! + 5! = 1 + 24 + 120 = 145, so 145 is a strong number.
Problem Statement
A strong number is a number that equals the sum of the factorials of its digits.
The classic example is 145, because 1! + 4! + 5! = 1 + 24 + 120 = 145.
Given a non-negative integer n, return true if it is a strong number and false
otherwise. Extract each digit, add its factorial to a running sum, and compare the
final sum with the original number.
Input: A single non-negative integer n.
Output: true if n is a strong number, otherwise false.
Examples
Input: 145
Output: true1! + 4! + 5! = 1 + 24 + 120 = 145, which equals the original number.
Input: 123
Output: false1! + 2! + 3! = 1 + 2 + 6 = 9, which is not 123.
Constraints
0 <= n <= 2,147,483,647 (fits in a 32-bit int)Digit factorials range only from 0! to 9!, so they are small and precomputable
Think Before You Code
Reveal the questions to ask yourself first
- What is the factorial of a single digit, and what is the largest it can be?
- Why must you keep the original number while extracting its digits?
- How would precomputing 0! through 9! simplify the inner work?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Sum the factorials of the digits and compare with the number.
- Copy
ninto a temporary variable and setsum = 0. - While the temporary value is not 0:
digit = temp % 10.sum += factorial(digit).temp = temp / 10.
- Return whether
sumequals the originaln.
A small helper computes the factorial of a single digit with a simple loop.
Dry Run
Walk through the example step by step
Checking n = 145:
temp | digit | digit! | sum
-----+-------+--------+-----
145 | 5 | 120 | 120
14 | 4 | 24 | 144
1 | 1 | 1 | 145
0 | — | — | 145
sum (145) == original (145) -> true.
Solution
Reveal the full Java solution
public class StrongNumber {
public static boolean isStrong(int n) {
if (n < 0) {
return false;
}
int temp = n;
int sum = 0;
while (temp != 0) {
int digit = temp % 10;
sum += factorial(digit);
temp /= 10;
}
return sum == n;
}
private static int factorial(int d) {
int result = 1;
for (int i = 2; i <= d; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
System.out.println(isStrong(145)); // true
System.out.println(isStrong(123)); // false
}
}
Each digit's factorial is at most 9! = 362880, so the running sum stays comfortably
within int for the values that can actually be strong numbers. Working on a copy of
n preserves the original for the final equality check, and the factorial helper
returns 1 for both 0 and 1 because the loop starts at 2.
O(d) digits, each with a constant-size factorial (digits are 0-9)Space: O(1)Common Mistakes
- Summing the digits themselves instead of their factorials.
- Destroying the original n during digit extraction and losing the comparison target.
Edge Cases to Test
- 1 and 2 are strong numbers because 1! = 1 and 2! = 2.
- 0 returns true only if you treat 0! contribution as 0; here 0 yields sum 0 == 0 (the loop skips).
Interview Follow-Ups
- How would you list every strong number below a given limit?
- How would precomputing a table of 0! through 9! speed up repeated checks?
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 →