Check Strong Number in Python

Beginner
⏱️ 10 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Number Theory

What You’ll Learn

A strong number (digital factorial) equals the sum of the factorials of its digits. Classic examples: 1, 2, 145, 40585. Non-examples: 10 (1!+0!=2), 99 (huge factorial sum). This tutorial covers a 0..9 factorial lookup, a live check, worked Python examples, edge cases, and complexity.

Definition

Digit factorials

Sum of d! for each digit equals n.

Lookup 0..9

Precompute

Avoid recomputing factorial each time.

145 Classic

1!+4!+5!

1 + 24 + 120 = 145.

1 and 2

Also strong

1! = 1 and 2! = 2.

Live Preview

Try 145 / 10

See digit factorial terms.

Not Armstrong

Powers vs !

Different digit tricks.

Introduction

A strong number equals the sum of the factorials of its digits. So 145 = 1! + 4! + 5! = 1 + 24 + 120, while 10 fails because 1! + 0! = 2.

Interviews love a small lookup table for 0! through 9!, then a digit loop with % 10 and // 10. That is fast, clear, and easy to dry-run on a whiteboard.

Why it matters?

It combines digit extraction with factorial basics — and shows why precomputing beats recomputing.

Key Highlights

Sum of d!

Equals the number.

Lookup Table

0! … 9! once.

1, 2, 145

In range 1..200.

vs Armstrong

Factorials, not powers.

In short: precompute fact[0..9], sum fact[digit] for every digit, and compare with n.

📝 Problem & Approach

Given a positive integer n, decide whether the sum of factorials of its digits equals n.

python
# 145 -> 1! + 4! + 5! = 1 + 24 + 120 = 145   strong
# 2   -> 2! = 2                               strong
# 10  -> 1! + 0! = 2                          not strong
# 99  -> 9! + 9! = 725760                     not strong

Inputs & Outputs

ItemTypeDescription
nintValue to test (n >= 1 in this tutorial).
ReturnboolTrue when sum of digit factorials equals n.
factlistLookup for 0! through 9!.

Minimal workflow

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

Method comparison

MethodIdeaNotes
Lookup + digit loopPrecompute 0..9, sum fact[d]Interview default
Range scanCall is_strong on each iLists 1 2 145 in 1..200
Trace termsPrint each d! contributionGreat for debugging

⚡ Quick Reference

GoalPattern
Lookupfact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
Next digitdigit = n % 10
Add factorialtotal += fact[digit]
Drop digitn //= 10
Verdictreturn total == original
Early stopif total > original: return False

📋 Check vs Range vs Trace

Same definition — different packaging.

Single check
is_strong(145)

Lookup + digit loop

Range
1..200

Finds 1 2 145

Trace
print d!

Shows each term

vs Armstrong
! vs ^

Factorials, not powers

Context

When This Problem Shows Up

Reach for a strong check when digit factorials meet equality.

  1. Interview warm-ups

    Definition + lookup + digit loop.

  2. Range listing

    Find strong values in a band.

  3. Factorial practice

    Pairs with factorial tutorials.

  4. Contrast Armstrong

    Same digit loop, different op.

  5. Not for 0

    Most beginner defs start at n >= 1.

Key benefit: one memorable formula — sum of digit factorials — with a tiny constant-size lookup.

🔮 Live Preview

Sums digit factorials with a 0..9 lookup and reports the strong verdict.

Use whole numbers n >= 1. Preview allows up to 1,000,000,000.

Live result
Press “Run check” to see the result.

Examples Gallery

Three complete Python programs — check 145, list strong numbers from 1 to 200, and print digit-factorial traces for candidates. Click View Output to reveal sample console results.

📚 Getting Started

A lookup table plus a digit loop is the interview-friendly approach.

Example 1 — Check a Single Number

Precompute 0!..9!, walk digits, and compare the factorial sum with the original value.

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.")

How It Works

Digits of 145 are 1, 4, and 5. Factorials are 1, 24, and 120, which sum to 145.

⚡ Hunting in a Range

Reuse the helper to list nearby strong values.

Example 2 — Strong Numbers from 1 to 200

Scan the band and print matches. Within 1..200 you only get 1, 2, and 145.

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()

How It Works

1 and 2 are trivial strong numbers; 145 is the first multi-digit hit. The next famous one, 40585, sits well above 200.

Example 3 — Trace Digit Factorials for Candidates

Print each digit’s factorial contribution so you can see why a value is strong or not.

python
FACT = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

def factorial_sum_terms(n: int):
    terms = []
    total = 0
    x = n
    while x > 0:
        d = x % 10
        total += FACT[d]
        terms.append(f"{d}!={FACT[d]}")
        x //= 10
    terms.reverse()
    return total, terms

for n in [2, 10, 145, 99]:
    total, terms = factorial_sum_terms(n)
    label = "strong" if total == n else "not strong"
    print(f"{n}: {' + '.join(terms)} = {total} -> {label}")

How It Works

10 fails because 0! is 1, not 0. 99 blows past the original value immediately because 9! is already huge.

🧠 How the Algorithm Decides

1

Build fact[0..9]

Precompute once; digits never need more.

Lookup
2

Extract digits

Use % 10 and // 10.

Loop
3

Add fact[digit]

Accumulate the factorial sum.

Sum
=

Compare with n

Equal means strong; otherwise not.

🔎 Worked Walkthrough — 145 vs 10

Compare a classic yes case with a common no case involving 0!.

nDigitsFactorial sumVerdict
1451, 4, 51 + 24 + 120 = 145Strong
222 = 2Strong
101, 01 + 1 = 2Not strong
999, 9362880 + 362880Not strong

Remember: 0! = 1, which trips people who expect zero.

Use Cases

Where strong checks show up beyond the interview prompt.

1. Interview Classics

Digit factorials equality.

Example: is_strong(145).

2. Range Listing

Find strong values in a band.

Example: 1 2 145.

3. Factorial Warm-up

Pairs with factorial tutorials.

Example: related links.

4. Contrast Armstrong

Same digits, different ops.

Example: FAQ.

5. Debugging Traces

Print each d! term.

Example: Example 3.

6. Next: Condense

Continue the interview chain.

Example: related CTA.

Pro Tip: open with “n equals the sum of factorials of its digits” and write the 0..9 table first.

Advantages

Why the lookup-table approach works well for beginners and interviews.

  1. 1. Tiny Constant Table

    Only ten factorials ever matter.

  2. 2. Easy to Trace

    Dry-run 145 on paper in seconds.

  3. 3. Fast Digit Loop

    O(digits) with O(1) extras.

  4. 4. Early Exit Option

    Stop if the running sum exceeds n.

Pro Tip: mention early-stop as an optional optimization after the clear baseline loop.

Usage Tips

Small habits that keep strong-number solutions interview-ready.

  1. 1. Save Original n

    You destroy n while extracting digits.

  2. 2. Precompute Once

    Never recompute factorial per digit.

  3. 3. Remember 0! = 1

    It is why 10 is not strong.

  4. 4. Know 1..200 Hits

    Expect 1, 2, and 145.

  5. 5. Contrast Armstrong

    Say the difference out loud in interviews.

Pro Tip: sanity-check 1, 2, 10, 145, and 99 — if those five behave, your logic is solid.

Common Pitfalls

Mistakes that commonly break strong-number programs.

  1. 1. Treating 0! as 0

    0! is 1 by definition.

    → Put 1 at fact[0].

  2. 2. Recomputing Factorial

    Nested factorial loops per digit.

    → Use a lookup list.

  3. 3. Comparing Against Destroyed n

    Forgetting to save original.

    → Keep original = n.

  4. 4. Confusing with Armstrong

    Using powers instead of factorials.

    → Say the difference explicitly.

  5. 5. Calling 0 Strong

    Outside this tutorial’s n >= 1 focus.

    → Follow the problem statement.

Edge Cases

Handle these before claiming the check is complete.

n = 0

Usually excluded

Most interview versions start from n >= 1.

n = 1, 2

Strong

1! = 1 and 2! = 2.

n = 10

Not strong

1! + 0! = 2.

Performance

Use a lookup

Do not recompute factorial often.

145

Classic yes

1! + 4! + 5! = 145.

40585

Larger famous case

Beyond the 1..200 list.

⚖️ Facts Worth Knowing

Handy follow-ups interviewers sometimes ask.

  • Also called digital factorial numbers. Same idea, different name.
  • Known base-10 examples. 1, 2, 145, and 40585.
  • 0! = 1. Critical for any number containing digit 0.
  • Not Armstrong. Armstrong uses digit powers; strong uses factorials.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Prove 145

  • 1! + 4! + 5!
  • Show = 145

2. Reject 10

  • Use 0! = 1
  • Confirm sum = 2

3. List 1..200

  • Reproduce Example 2
  • Expect 1 2 145

4. Trace 40585

  • Optional stretch
  • Verify digit factorials

Notes

  • Definition: strong means the sum of digit factorials equals n.
  • Lookup: precompute factorials for 0..9 once.
  • Range check: in 1..200 you should get 1 2 145.
  • Optimization: early-stop if the running sum exceeds the original number. Still prefer the clear baseline first.

Quick Takeaway: n is strong when sum(fact[digit] for each digit) == n.

⏱️ Time and Space Complexity

TaskTimeExtra space
Check one nO(d) (d = digits)O(1)
Scan 1..UO(U log U)O(1)
Lookup tablebuild once10 integers

Digit count grows like log10 n, so a single check is essentially linear in the number of digits.

Wrap Up

🎉 Conclusion

A strong number equals the sum of the factorials of its digits. Precompute 0! through 9!, walk the digits, and compare — remembering that 0! = 1.

Practice the three examples above, then continue to condensing a number.

Sum of digit factorials equals n.

💡 Best Practices

✅ Do

  • Precompute 0!..9!
  • Save original n
  • Treat 0! as 1
  • Dry-run 145
  • Know 1 2 145 in 1..200

❌ Don’t

  • Recompute factorial every digit
  • Confuse with Armstrong
  • Assume 0! = 0
  • Compare after destroying n
  • Ignore the problem’s n >= 1 rule

Key Takeaways

Knowledge Unlocked

Five things to remember about strong numbers

Classify numbers whose digit factorials sum to themselves.

5
Core concepts
T 02

Table

0!..9! lookup

Method
0 03

Trap

0! = 1

Edge
1 04

List

1 2 145

Check
O 05

Cost

O(digits)

Analysis

❓ Frequently Asked Questions

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.
Another famous strong number: 4!+0!+5!+8!+5! = 40585.
Yes. If the running factorial sum exceeds n, it cannot be strong.
State the definition, show the 0..9 lookup, then dry-run 145.

Did you Know? 🔊

Strong numbers are also called digital factorial numbers. In base 10, the classic examples are 1, 2, 145, and 40585.

Continue to Condense a Number

Learn how to repeatedly sum digits until a single digit remains.

Condense a number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful