Strong Number in Python: Sum of Factorials of Digits

A strong number is a nonnegative integer equal to the sum of the factorials of its digits. For example, 145 = 1! + 4! + 5! = 1 + 24 + 120.

Interactive Companion Jupyter Notebook

Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.

View on GitHub Open in Google Colab
On this page — Show page sections— Hide page sections

First working example

from math import factorial
n = 145
total = sum(factorial(int(ch)) for ch in str(n))
print("Sum of digit factorials:", total)
print("Strong number:", total == n)

Output:

Sum of digit factorials: 145
Strong number: True

The digit factorial is different from the factorial of the whole number.

Original manual factorial helper

The original tutorial implements factorial using a loop. This helper is useful for understanding how the digit test works.

def my_factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result
print(my_factorial(5))

Output:

120

Check a number entered by the user

Convert each character to a digit and compare with the original positive integer.

from math import factorial
raw = input("Enter a nonnegative whole number: ").strip()
if not raw.isdecimal():
    print("Enter digits only")
else:
    n = int(raw)
    total = sum(factorial(int(digit)) for digit in str(n))
    print("Sum:", total)
    print("Strong number:", total == n)

Output:

Sum: 145
Strong number: True

Reusable strong-number function

The result is a Boolean, which can be reused in filters and loops.

from math import factorial
def is_strong(n):
    return n >= 0 and sum(factorial(int(ch)) for ch in str(n)) == n
for number in (1, 2, 145, 146, 40585):
    print(number, is_strong(number))

Output:

1 True
2 True
145 True
146 False
40585 True

List strong numbers in a range

The original lesson found 145 and 40585 between 10 and 100000. The improved function also handles the single-digit numbers 1 and 2.

from math import factorial
facts = [factorial(i) for i in range(10)]
def strong(n):
    return n >= 0 and sum(facts[int(ch)] for ch in str(n)) == n
print([n for n in range(1, 100000) if strong(n)])

Output:

[1, 2, 145, 40585]

Why 0 is not strong

Because 0! = 1, zero does not equal the factorial sum of its digits.

from math import factorial
print("0!:", factorial(0))
print("0 is strong:", factorial(0) == 0)

Output:

0!: 1
0 is strong: False

Show the calculation

Display each factorial term so the equality becomes easy to inspect.

from math import factorial
n = 145
terms = [factorial(int(d)) for d in str(n)]
print(" + ".join(map(str, terms)), "=", sum(terms))

Output:

1 + 24 + 120 = 145

Common mistakes and troubleshooting

  • Run each example in Python 3.9 or newer. Indentation is significant in loops and functions.
  • Remember that input() returns a string. Convert and validate the value before doing arithmetic.
  • Check boundary values such as zero, negative numbers, small inputs and empty ranges.
  • When comparing outputs, note whether a code sample uses fixed values or user input.

Practice exercises

Try 2, 145, 146 and 40585, then list strong numbers below 100000. Use the companion practice notebook for runnable examples and self-checks.

Frequently asked questions

Is 145 a strong number?

Yes: 1! + 4! + 5! = 145.

Is 0 a strong number?

No, 0! is 1.

Is a strong number the same as an Armstrong number?

No. Strong numbers use digit factorials; Armstrong numbers use powers.

Plus2Net Python Basics podcast playlist

Other related resources

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer