Table of contents
1.
Introduction
2.
What is a Strong Number?
3.
Problem Approach to Finding Strong Numbers
4.
Python Program to Find Strong Number
4.1.
Python
5.
Strong Number Using the for Loop
5.1.
Python
6.
Python Program to Find Strong Number Using Factorial Function
6.1.
Python
7.
Frequently Asked Questions
7.1.
What makes a number a strong number?
7.2.
How can I check for strong numbers in a range of numbers using Python?
7.3.
Are there many strong numbers?
8.
Conclusion
Last Updated: Aug 13, 2025
Easy

Strong Numbers in Python

Author Rahul Singh
0 upvote

Introduction

Strong numbers hold a unique position in programming, particularly when practicing numerical operations & algorithmic challenges in Python. Initially, these numbers might just seem like any set of digits. However, they are special due to their mathematical properties which involve factorials. 

Strong Numbers in Python

This article will help us in understanding what strong numbers are, how to identify them using Python, & the steps involved in writing efficient code to detect these numbers. 

What is a Strong Number?

A strong number is a specific type of number in mathematics that has an interesting property: the sum of the factorials of its digits equals the number itself. To understand this better, let's break down the terms involved. A factorial, represented as n!, is the product of all positive integers up to n. For example, the factorial of 4 (4!) is 4 × 3 × 2 × 1, which equals 24.

Let's consider an example to illustrate a strong number. Take the number 145:

  • The factorial of 1 is 1.
     
  • The factorial of 4 is 24 (4 × 3 × 2 × 1).
     
  • The factorial of 5 is 120 (5 × 4 × 3 × 2 × 1).
     

When you add these factorials together (1 + 24 + 120), the sum is 145, which is the original number, making 145 a strong number.

Identifying strong numbers can be an interesting exercise in programming & provides a good opportunity to practice loops & conditional statements in Python. This concept not only sharpens your understanding of factorials but also enhances your ability to manipulate numbers & perform calculations in your code.

Problem Approach to Finding Strong Numbers

To solve the problem of identifying strong numbers using Python, we need a clear strategy. Here’s a step-by-step approach that we can follow:

  1. Input a Number: First, we need to have a number to check. This can be provided by the user or predefined in the code.
     
  2. Separate the Digits: We break the number into its individual digits. For example, if the number is 145, we separate it into 1, 4, and 5.
     
  3. Calculate Factorials: For each digit, calculate the factorial. Python's math library has a function factorial() that can be used directly, or you can write your own function to compute it.
     
  4. Sum the Factorials: Add up all the factorial values of the digits.
     
  5. Compare Sum to Original Number: Check if the sum of these factorials is equal to the original number. If it is, the number is a strong number.
     
  6. Output the Result: Display whether the number is strong or not.

Python Program to Find Strong Number

Here's how you can write a Python program to determine if a given number is a strong number:

  • Python

Python

# Import the factorial function from the math module

from math import factorial

# Function to check if a number is a strong number

def is_strong_number(number):

   # Convert the number to string to iterate over each digit

   digits = str(number)

   # Calculate the sum of factorials of all digits

   sum_of_factorials = sum(factorial(int(digit)) for digit in digits)

   # Check if the sum of factorials is equal to the original number

   return sum_of_factorials == number

# Example usage

number_to_check = 145

if is_strong_number(number_to_check):

   print(f"{number_to_check} is a strong number.")

else:

   print(f"{number_to_check} is not a strong number.")
You can also try this code with Online Python Compiler
Run Code

Output

145 is a strong number.


In this program:

  • We first import the factorial function from the Python math module, which allows us to easily compute the factorial of each digit.
     
  • We define a function is_strong_number that takes a number as an argument.
     
  • Inside this function, the number is converted into a string to extract each digit individually.
     
  • We then compute the sum of the factorials of these digits using a generator expression within the sum() function.
     
  • Finally, the function returns True if the sum of the factorials is equal to the number itself, indicating that it is a strong number; otherwise, it returns False.
     
  • The example usage shows how to call this function and print whether a specific number is a strong number.

Strong Number Using the for Loop

To enhance our understanding and provide an alternative method for finding strong numbers, we can use the for loop in Python. This approach allows us to manually compute the factorial for each digit and accumulate the sum. Here’s how you can implement it:

  • Python

Python

# Function to calculate factorial of a number

def factorial(n):

   result = 1

   for i in range(1, n + 1):

       result *= i

   return result

# Function to check if a number is a strong number using the for loop

def is_strong_number(number):

   sum_of_factorials = 0

   original_number = number

   while number > 0:

       digit = number % 10  # Get the last digit

       sum_of_factorials += factorial(digit)

       number //= 10  # Remove the last digit

   return sum_of_factorials == original_number

# Example usage

number_to_check = 145

if is_strong_number(number_to_check):

   print(f"{number_to_check} is a strong number.")

else:

   print(f"{number_to_check} is not a strong number.")
You can also try this code with Online Python Compiler
Run Code

Output

145 is a strong number.


This version of the code does the following:
 

  • It includes a custom factorial function that calculates the factorial of a given number using a for loop. This function multiplies each number from 1 up to n to get the factorial.
     
  • The is_strong_number function initializes a sum accumulator, sum_of_factorials, to 0.
     
  • It uses a while loop to process each digit of the given number:
     
  • Extract the last digit using the modulus operator (%).
     
  • Add the factorial of this digit to sum_of_factorials.
     
  • Remove the last digit from the number by integer division (//).
     
  • After the loop, it compares the sum of the factorials with the original number to determine if it is a strong number.
     
  • The example usage section tests the number 145 and prints out the result.

Python Program to Find Strong Number Using Factorial Function

To optimize our previous Python programs for finding strong numbers, we can use a more efficient approach with a pre-computed factorial function. This method reduces the computational overhead by avoiding repeated calculations of factorials for each digit, which is especially useful when checking multiple numbers or larger numbers. Here's how to implement it:

  • Python

Python

# Precomputed factorials for digits 0-9

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

# Function to check if a number is a strong number using precomputed factorials

def is_strong_number(number):

   sum_of_factorials = 0

   original_number = number

   while number > 0:

       digit = number % 10  # Get the last digit

       sum_of_factorials += factorials[digit]

       number //= 10  # Remove the last digit




   return sum_of_factorials == original_number

# Example usage

number_to_check = 145

if is_strong_number(number_to_check):

   print(f"{number_to_check} is a strong number.")

else:

   print(f"{number_to_check} is not a strong number.")
You can also try this code with Online Python Compiler
Run Code

Output

145 is a strong number.

This version of the program includes the following features:

  • Precomputed Factorials: We have an array called factorials that stores the factorial of each digit from 0 to 9. This array is used to look up the factorial value directly, which is faster than computing it each time.
     
  • Efficient Calculation: The function is_strong_number operates similarly to the previous example, but instead of computing the factorial each time, it retrieves the value from the factorials array.
     
  • While Loop: The function still uses a while loop to process each digit of the input number, accumulating the sum of the factorials, and then compares it with the original number to determine if it's a strong number.
     
  • Example Usage: It provides an example that tests whether the number 145 is a strong number and prints the result accordingly.
     

Note -: This approach is beneficial for understanding how pre-computation can speed up programs, particularly when dealing with repetitive calculations.

Frequently Asked Questions

What makes a number a strong number?

A strong number is defined as a number where the sum of the factorials of its digits equals the number itself. For example, 145 is a strong number because 1! + 4! + 5! = 145.

How can I check for strong numbers in a range of numbers using Python?

You can use a loop to iterate through a range and apply the is_strong_number function to each number. This helps you identify all the strong numbers within that range.

Are there many strong numbers?

No, strong numbers are quite rare. Some known strong numbers are 1, 2, 145, and 40585. They are limited because the factorial grows very rapidly compared to the growth of the number itself.

Conclusion

In this article, we have learned about strong numbers and how to determine if a number qualifies as a strong number using Python. We talked about different methods, starting from the basic implementation using factorials calculated on-the-fly, to more advanced techniques like precomputing factorials for efficiency. Through code examples, we showed how to implement these concepts practically, enhancing your problem-solving skills in Python programming. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass