How to implement rising factorial in Python

In mathematics, a rising factorial is represented by x(n)x^{(n)}, where x(n)=x×(x+1)××(x+n1)x^{(n)}=x×(x+1)×…×(x+n−1). It is the factorial function in a broader sense. We can use a loop or recursion to create a simple function in Python that implements a rising factorial.

Algorithm for rising factorial

To implement a rising factorial in Python, we can create a simple function using a loop or recursion.

Using a loop

Here’s the loop-based rising factorial implementation:

def rising_factorial(x, n):
result = 1
for i in range(n):
result *= x + i
return result
# Example usage
x = 3
n = 4
result = rising_factorial(x, n)
print(f"{x}^{n} =", result)

Explanation

  • Lines 1–5: The rising factorial of x with respect to n will be computed by this function.

    • Line 2: Sets the value of a variable called result to 1. Throughout the loop, the product will be accumulated using this variable.

    • Lines 3–5: Initiates a loop that iterates n times, where i takes on values from 0 to n-1. Within the loop, update the result by multiplying it with the expression x + i. By multiplying consecutive terms, the rising factorial is accumulated. The function returns the rising factorial’s final calculated result at the end of the loop.

  • Lines 9–12: The values of x and n are set to 3 and 4, respectively. The rising_factorial function is then called with these values, and the result is stored in the variable result. The function computes the rising factorial, multiplying consecutive terms starting from the base x up to x+n-1. The base, exponent, and calculated rising factorial are finally displayed in an f-string printout of the outcome.

Using recursion

Here’s the recursion-based rising factorial implementation:

def rising_factorial_recursive(x, n):
if n == 0:
return 1
else:
return x * rising_factorial_recursive(x + 1, n - 1)
# Example usage
x = 3
n = 4
result_recursive = rising_factorial_recursive(x, n)
print(f"{x}^{n} =", result_recursive)

Explanation

  • Lines 1–5: The rising factorial of x with respect to n will be computed by this function.

    • Lines 2–3: This is the base case for the recursion, where if n is 0, the function returns 1, effectively terminating the recursion.

    • Lines 4–5: The function recursively calls itself with the base x + 1 and the exponent n - 1. It multiplies x with the result of this recursive call, effectively building the rising factorial term by term.

  • Lines 9–10: Assigns the value 3 and 4 to the variable x and n. If n is not 0, the function returns the result of multiplying x with the rising factorial of x + 1 and n - 1. This recursive call iteratively calculates the rising factorial by reducing the problem to smaller subproblems until n becomes 0.

  • Line 11: Calls the rising_factorial_recursive function with the provided values of x and n. The result of the rising factorial calculation is stored in the variable result_recursive.

  • Line 12: Prints the result of the rising factorial calculation using an f-string. Displays the base (x), the exponent (n), and the calculated rising factorial result (result_recursive).

Key points

Here are some important points:

Validity of parameters

  • Verify that the parametersxxandnnare appropriate for the use case. It is required thatnnbe a positive number.

Efficiency considerations

  • The recursive implementation may lead to a large recursion depth for large values ofnn. In such cases, an iterative approach may be more efficient.

  • Take into account the algorithm’s temporal complexity, particularly when working with big values of nn.

Error handling

  • Put error handling in place to deal with situations where incorrect input is given, such as whennnhas negative values.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved