5 Best Ways to Calculate Inverse Factorial in Python

πŸ’‘ Problem Formulation: Inverse factorial in Python involves finding the integer n such that n! is equal to a given number, if such an integer exists. For example, if the input is 120, the desired output is 5 because 5! equals 120. This article discusses how to calculate the inverse of a factorial.

Method 1: Iterative Approach

Using a simple iterative approach, we can calculate the inverse factorial by incrementing a value from 1 and multiplying it with a running total until the total reaches or exceeds the input number. This method’s simplicity makes it easily understandable and very straightforward in implementation.

Here’s an example:

def inverse_factorial_iteratively(num):
    if num == 1: return 1
    i = 2
    factorial = 1
    while factorial < num:
        factorial *= i
        if factorial == num:
            return i
        i += 1
    return None


Output: 5

This snippet defines a function inverse_factorial_iteratively that takes a number as an argument and calculates its inverse factorial using a loop. The function returns the value of i for which the running product equals the input number if found, or None if no integer satisfies the condition.

Method 2: Binary Search Approach

To optimize the search, a binary search methodology can be used. This is more efficient than the iterative approach for large numbers as it reduces the time complexity by narrowing down the potential candidates in a logarithmic fashion instead of linear increments.

Here’s an example:

from math import factorial

def inverse_factorial_binary_search(num):
    if num == 1: return 1
    low, high = 0, num
    while low < high:
        mid = (low + high) // 2
        mid_factorial = factorial(mid)
        if mid_factorial == num:
            return mid
        elif mid_factorial < num:
            low = mid + 1
            high = mid
    return None


Output: 5

This code snippet leverages the factorial function from Python’s math module to implement a binary search for the inverse factorial. It steadily narrows the search range until it finds the exact match for the given input number or concludes there is no integer whose factorial is equal to the input.

Method 3: Using Gamma Function

The Gamma function Ξ“(n) is a generalization of the factorial function, which works for real numbers and satisfies Ξ“(n) = (n-1)! for natural numbers n. By estimating the inverse Gamma function, one can determine the inverse factorial.

Here’s an example:

from scipy.special import gammaincinv
from math import floor

def inverse_factorial_gamma(num):
    if num == 1: return 1
    return floor(gammaincinv(1, num))


Output: 5

This code utilizes the inverse incomplete gamma function gammaincinv from the scipy.special package to calculate the inverse factorial. The result from gammaincinv is floored to give the nearest integer solution which serves as the inverse factorial if an exact integer solution exists.

Method 4: Lookup Table

If we are working within a known range of factorials, we can pre-calculate and store the factorial values in a lookup table. Then, we can find the inverse factorial by searching for the input number in this table. This method provides constant-time retrieval but is limited by the range of the pre-calculated values.

Here’s an example:

factorials = {1: 1, 2: 2, 6: 3, 24: 4, 120: 5}  # This can be extended

def inverse_factorial_lookup(num):
    return factorials.get(num, None)


Output: 5

The inverse_factorial_lookup function simply retrieves the inverse factorial from a pre-populated dictionary, with factorial values as keys and their respective integers as values. The retrieval is extremely quick but limited to the range of values in the dictionary.

Bonus One-Liner Method 5: Math Module with Exception Handling

Combining the iterative approach with Python’s math module can yield a one-liner that computes the inverse factorial, catching exceptions if no inverse factorial exists. This is concise but may not be the most efficient for larger numbers.

Here’s an example:

import math

inverse_factorial_oneliner = lambda num: next((i for i in range(1, num) if math.factorial(i) == num), None)


Output: 5

The one-liner defines a lambda function that uses a generator expression with the math.factorial method to find the inverse factorial. The next() function is used to fetch the first result that matches the condition, with None as a default if no match is found.


  • Method 1: Iterative Approach. Simple and easy to implement. Inefficient for large numbers as it may take many iterations.
  • Method 2: Binary Search Approach. Faster than the iterative approach for large numbers. Requires the use of Python’s math module.
  • Method 3: Using Gamma Function. Works for real numbers and can find nearest integers. Depends on external libraries like SciPy.
  • Method 4: Lookup Table. Offers constant-time retrieval for known ranges. Not scalable for very large numbers or ranges.
  • Bonus Method 5: Math Module with Exception Handling. Elegant one-liner for small to medium-sized inputs. Performance decreases with larger input values.