- Check
- 1! + 4! + 5! = 145
Check Strong Number in Java
What you’ll learn
- Strong number definition via digit factorial sums.
- Fast digit factorial lookup for
0..9. - Single-value check and range listing in Java.
- How to reason about complexity using number of digits.
Overview
A strong number equals the sum of factorials of its digits. Example: 145 = 1! + 4! + 5! = 145. Classic values are 1, 2, 145, 40585.
Two Java programs
Check one value and list strong numbers from 1 to 200.
Live preview
See factorial-expression sum and verdict instantly.
Simple optimization
Precompute factorials for digits 0 to 9 once.
Prerequisites
Digit extraction, factorial basics, and loop traversal of numbers.
- Use
% 10and/ 10to process digits. - Precompute factorial values for digits 0 through 9.
What is a strong number?
If sum of factorials of all decimal digits equals the number itself, the number is strong.
This is also called a digital factorial number in many interview sets.
Quick examples
- Sum
- 1!+2!+3! = 9
- Reason
- 2! = 2
Live preview
Type a positive integer to see the factorial sum of its digits and final verdict.
Algorithm
Prepare factorial lookup
Store factorials for digits 0..9 once.
Extract digits
Use modulo/division to process each digit.
Compare sums
If factorial-digit sum equals original, it is strong.
📜 Pseudocode
fact[0..9] = {1,1,2,6,24,120,720,5040,40320,362880}
function isStrong(n):
sum = 0
x = n
while x > 0:
digit = x mod 10
sum += fact[digit]
x = floor(x / 10)
return sum == nCheck one number
public class Main {
static boolean isStrongNumber(int n) {
int[] fact = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int original = n;
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += fact[digit];
n /= 10;
}
return sum == original;
}
public static void main(String[] args) {
int number = 145;
if (isStrongNumber(number)) {
System.out.println(number + " is a Strong Number.");
} else {
System.out.println(number + " is not a Strong Number.");
}
}
}Strong numbers from 1 to 200
public class Main {
static boolean isStrongNumber(int n) {
int[] fact = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int original = n;
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += fact[digit];
n /= 10;
}
return sum == original;
}
public static void main(String[] args) {
System.out.println("Strong Numbers in the Range 1 to 200:");
for (int i = 1; i <= 200; i++) {
if (isStrongNumber(i)) {
System.out.print(i + " ");
}
}
}
}Optimization
Lookup table. Precompute 0! through 9! once, then each digit contribution is O(1).
Short-circuit. Optional early stop when running sum already exceeds original.
Interview: mention lookup-table win and digit-count complexity.
❓ FAQ
Input/output examples
| Input n | Output |
|---|---|
145 | Strong number |
123 | Not strong |
2 | Strong number |
Edge cases and pitfalls
Common issues are handling zero and recomputing factorial repeatedly.
Usually not strong
Because 0! = 1, the factorial sum does not equal 0 in this variant.
Avoid repeated factorial work
Use the digit-factorial lookup table instead of recalculating each time.
Use wider type if needed
For very large ranges, use long for safer accumulation.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| Single strong check | O(d) where d is number of digits | O(1) |
| Range 1..U scan | O(U log U) | O(1) |
Summary
- Definition: strong means sum of
digit!equals the number. - Code: use
fact[10]and digit extraction with% 10. - Range 1..200:
1, 2, 145.
Classic strong numbers in base-10 are 1, 2, 145, and 40585.
9 people found this page helpful
