Check if a Number is a Strong Number

problem-solving
Published

March 7, 2024

Strong numbers, also known as strong perfect numbers, are numbers whose sum of the factorial of their digits is equal to the number itself. This sounds complex, but it’s surprisingly straightforward to implement in Python. Let’s look at how to write a function to determine if a number is a strong number.

Understanding Strong Numbers

Before diving into the Python code, let’s solidify our understanding with an example. Consider the number 145:

1! + 4! + 5! = 1 + 24 + 120 = 145

Since the sum of the factorials of its digits equals the number itself, 145 is a strong number. Conversely, a number like 123 is not a strong number because 1! + 2! + 3! = 1 + 2 + 6 = 9 ≠ 123.

Python Implementation

Here’s a Python function that efficiently checks if a given number is a strong number:

import math

def is_strong_number(num):
    """
    Checks if a number is a strong number.

    Args:
      num: The number to check.

    Returns:
      True if the number is a strong number, False otherwise.
    """
    original_num = num
    sum_of_factorials = 0

    while num > 0:
        digit = num % 10
        sum_of_factorials += math.factorial(digit)
        num //= 10

    return sum_of_factorials == original_num


#Example Usage
print(is_strong_number(145))  # Output: True
print(is_strong_number(123))  # Output: False
print(is_strong_number(40585)) # Output: True

This function first saves the original number. Then, it iterates through each digit of the number using the modulo operator (%) and integer division (//=) to extract each digit. It calculates the factorial of each digit using the math.factorial() function and adds it to the sum_of_factorials. Finally, it compares this sum to the original number. If they are equal, the number is a strong number; otherwise, it’s not.

Optimizations and Considerations

While the above code is functional and easy to understand, there’s room for optimization for extremely large numbers. For instance, you could pre-calculate factorials up to a certain limit and store them in a dictionary to avoid recalculating them repeatedly. However, for numbers within a reasonable range, the current implementation is perfectly sufficient.

Exploring Strong Numbers Further

This blog post provides a foundational understanding of strong numbers and how to identify them using Python. You can expand on this by exploring algorithms to find all strong numbers within a given range or examine the mathematical properties of strong numbers themselves. There are relatively few strong numbers, making them an interesting topic for mathematical exploration.