5 Best Ways to Check if a Number is a Strong Number in Python

Rate this post

πŸ’‘ Problem Formulation: In number theory, a Strong number (or factorial number) is a number whose sum of the factorial of digits is equal to the original number. For example, if the input number is 145, the output should indicate it’s a Strong number because 1! + 4! + 5! = 145.

Method 1: Using Iteration

This method checks the strength of a number by iterating through each digit, computing its factorial, and summing them up to compare with the original number. It’s a straightforward brute-force approach.

Here’s an example:

def factorial(n):
    return 1 if n == 0 else n * factorial(n - 1)

def is_strong_number(num):
    return num == sum(factorial(int(d)) for d in str(num))

# Test the function
print(is_strong_number(145))

Output: True

This code defines a factorial function and a function to check if a number is strong. It converts the number into a string, iterates through each character, converts it back to an integer, computes the factorial, and sums these factorials. The outcome is compared with the original number.

Method 2: Using Memoization

This technique leverages memoization to store previously calculated factorials which reduces the computational time for repeated digits.

Here’s an example:

factorials = {0: 1}

def factorial(n):
    if n not in factorials:
        factorials[n] = n * factorial(n - 1)
    return factorials[n]

def is_strong_number(num):
    return num == sum(factorial(int(d)) for d in str(num))

# Test the function
print(is_strong_number(145))

Output: True

In this improved solution, a dictionary called ‘factorials’ caches the factorial results. This optimization is particularly helpful if you’re testing multiple numbers repeatedly.

Method 3: Using an Iterative Factorial Function

This method relies on an iterative factorial function instead of a recursive one. It’s an optimized approach, which is generally faster and avoids the recursion limit in Python.

Here’s an example:

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

def is_strong_number(num):
    return num == sum(factorial(int(d)) for d in str(num))

# Test the function
print(is_strong_number(145))

Output: True

This code defines an iterative factorial function which multiplies numbers up to the given integer. The main advantage is efficiency, as it doesn’t involve the overhead of recursive function calls.

Method 4: Using Library Functions

This method makes use of Python’s standard library functions to calculate factorials, which is typically optimized and compiled in C for efficiency.

Here’s an example:

from math import factorial

def is_strong_number(num):
    return num == sum(factorial(int(d)) for d in str(num))

# Test the function
print(is_strong_number(145))

Output: True

Using the factorial function from Python’s math module provides a clean and efficient way to perform factorial calculations without implementing the logic from scratch.

Bonus One-Liner Method 5: Functional Programming Approach

This one-liner method employs a functional programming style, utilizing map, sum, and the factorial function from the math module in a single expression.

Here’s an example:

from math import factorial

def is_strong_number(num):
    return num == sum(map(factorial, map(int, str(num))))

# Test the function
print(is_strong_number(145))

Output: True

The example uses map to apply the factorial function to each digit of the number after converting it to an integer. It’s a concise approach but may sacrifice some readability for brevity.

Summary/Discussion

  • Method 1: Iterative Brute-Force. Simple to understand. May be inefficient for large numbers due to repeated calculations.
  • Method 2: Memoized Factorial. More efficient for numbers with repeated digits thanks to caching. Slightly more complex due to memoization logic.
  • Method 3: Iterative Factorial Function. More memory-efficient than recursion and avoids maximum recursion depth issues. Faster for large numbers but less pythonic.
  • Method 4: Standard Library Utilization. Highly efficient and clean code using optimized library functions. Relies on Python’s standard library.
  • Method 5: Functional Programming One-Liner. Extremely concise, showcasing the power of Python’s functional programming capabilities. Can decrease readability.