Check Strong Number in Python
What you’ll learn
- What a strong number (digital factorial) means.
- How to compute digit factorials quickly with a lookup table.
- How to check one value and list strong numbers in a range.
Overview
A strong number equals the sum of factorials of its digits. Example: 145 = 1! + 4! + 5!.
Prerequisites
Know loops, integer division, and modulo operations.
What is a strong number?
A number is strong when the sum of factorials of its digits is equal to the number itself.
For 145: 1! + 4! + 5! = 1 + 24 + 120 = 145.
Live preview
Algorithm
1
Precompute 0! to 9!
Store them in a list for O(1) lookup.
2
Extract digits
Use % 10 and // 10 to process each digit.
3
Compare sums
Strong if factorial sum equals original number.
📜 Pseudocode
Pseudocode
fact = [1,1,2,6,24,120,720,5040,40320,362880]
function isStrong(n):
sum = 0
x = n
while x > 0:
d = x mod 10
sum = sum + fact[d]
x = floor(x / 10)
return sum == n 1
Check a single number
python
def is_strong_number(n: int) -> bool:
fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
original = n
total = 0
while n > 0:
digit = n % 10
total += fact[digit]
n //= 10
return total == original
number = 145
if is_strong_number(number):
print(f"{number} is a Strong Number.")
else:
print(f"{number} is not a Strong Number.") 📤 Output
145 is a Strong Number.
2
Strong numbers from 1 to 200
python
def is_strong_number(n: int) -> bool:
fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
original = n
total = 0
while n > 0:
digit = n % 10
total += fact[digit]
n //= 10
return total == original
print("Strong Numbers in the Range 1 to 200:")
for i in range(1, 201):
if is_strong_number(i):
print(i, end=" ")
print() 📤 Output
Strong Numbers in the Range 1 to 200: 1 2 145
Optimization
Lookup table: Precompute factorials for 0..9 once.
Early stop: You can stop if running sum exceeds original number.
❓ FAQ
A strong number equals the sum of the factorials of its digits. Example: 145 = 1! + 4! + 5!.
Yes. 1! = 1 and 2! = 2.
Usually no for beginner definitions focused on positive integers, because 0! = 1.
Digits are only 0 to 9, so precomputing 0! to 9! avoids repeated factorial calculations.
1, 2, and 145.
No. Armstrong numbers use powers of digits; strong numbers use factorials of digits.
🔄 Input / output examples
| n | Strong? | Reason |
|---|---|---|
| 145 | Yes | 1! + 4! + 5! = 145 |
| 2 | Yes | 2! = 2 |
| 10 | No | 1! + 0! = 2 |
| 99 | No | 9! + 9! = 725760 |
Edge cases and pitfalls
n = 0
Usually excluded
Most interview versions start from n >= 1.
Performance
Do not recompute factorial often
Use lookup list for 0..9.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| Check one n | O(d), d = number of digits | O(1) |
| Scan 1..U | O(U log U) | O(1) |
Summary
- Strong means sum of digit factorials equals n.
- Use precomputed factorial lookup.
- In range 1..200: 1, 2, 145.
Did you know?
Strong numbers are also called digital factorial numbers. In base 10, the classic examples are 1, 2, 145, and 40585.
9 people found this page helpful
