0
0
PythonProgramBeginner · 2 min read

Python Program to Find Strong Number

A strong number is a number whose sum of the factorials of its digits equals the number itself; in Python, you can check this by calculating sum(math.factorial(int(digit)) for digit in str(number)) == number.
📋

Examples

Input145
Output145 is a strong number
Input123
Output123 is not a strong number
Input1
Output1 is a strong number
🧠

How to Think About It

To find if a number is strong, break it into digits, find the factorial of each digit, add these factorials, and compare the sum to the original number. If they match, the number is strong.
📐

Algorithm

1
Get the input number.
2
Convert the number to a string to access each digit.
3
Calculate the factorial of each digit and sum them.
4
Compare the sum with the original number.
5
If equal, print that the number is strong; otherwise, print it is not.
💻

Code

python
import math

def is_strong_number(num):
    total = sum(math.factorial(int(digit)) for digit in str(num))
    if total == num:
        print(f"{num} is a strong number")
    else:
        print(f"{num} is not a strong number")

# Example usage
is_strong_number(145)
Output
145 is a strong number
🔍

Dry Run

Let's trace the number 145 through the code

1

Convert number to string

str(145) -> '145'

2

Calculate factorial of each digit

factorial(1) = 1, factorial(4) = 24, factorial(5) = 120

3

Sum factorials

1 + 24 + 120 = 145

4

Compare sum with original number

145 == 145 -> True

5

Print result

"145 is a strong number"

DigitFactorial
11
424
5120
💡

Why This Works

Step 1: Breaking number into digits

Converting the number to a string lets us look at each digit separately using str(num).

Step 2: Calculating factorial

We use math.factorial() to find the factorial of each digit, which is the product of all positive integers up to that digit.

Step 3: Comparing sum to original

If the sum of all digit factorials equals the original number, it means the number is strong.

🔄

Alternative Approaches

Manual factorial calculation
python
def factorial(n):
    result = 1
    for i in range(2, n+1):
        result *= i
    return result

def is_strong_number(num):
    total = sum(factorial(int(d)) for d in str(num))
    if total == num:
        print(f"{num} is a strong number")
    else:
        print(f"{num} is not a strong number")

is_strong_number(145)
Calculates factorial manually without importing math, useful if external libraries are restricted.
Using recursion for factorial
python
def factorial(n):
    if n == 0 or n == 1:
        return 1
    return n * factorial(n-1)

def is_strong_number(num):
    total = sum(factorial(int(d)) for d in str(num))
    if total == num:
        print(f"{num} is a strong number")
    else:
        print(f"{num} is not a strong number")

is_strong_number(145)
Uses recursion to calculate factorial, which is elegant but can be slower for large digits.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit once, calculating factorial which is O(1) for digits 0-9, so overall O(d) where d is number of digits.

Space Complexity

Uses constant extra space for sum and factorial calculations, so O(1).

Which Approach is Fastest?

Using math.factorial() is fastest and simplest; manual or recursive factorial methods add overhead.

ApproachTimeSpaceBest For
Using math.factorialO(d)O(1)Simplicity and speed
Manual factorial loopO(d)O(1)No imports allowed
Recursive factorialO(d)O(1)Learning recursion
💡
Use math.factorial() for clean and fast factorial calculation in Python.
⚠️
Forgetting to convert digits from string to integer before calculating factorial causes errors.