Superfactorial of a number in Python
In this tutorial, we are going to write a program to find the superfactorial of a number in Python. A superfactorial of a given number n would be the product of first n factorials.
Let’s see how we can write a Python code to get the superfactorial value of a given number.
Find the Superfactorial of a number in Python
Superfactorial of a number n would be:
sf(n) = 1! x 2! x 3! .....n!
To write a program to find the superfactorial of a number, we can use a loop iterating from 0 to n and factorial() function to find factorial of each element in the loop. The factorial() method is defined in the Python math module. Follow the link for more about math functions in Python.
The code implementation of the above formula has been given in the below program.
import math n=4 sup = 1 for i in range(n+1): sup = sup * math.factorial(i) print(sup)
The above code gives the output as:
There is another way to write the program to find the superfactorial of a number. We can represent superfactorial of a number like the below formula as well:
sf(n) = 1^n * 2^(n-1) * ....n^1
Now we can write a python program based on this formula. Go through the code carefully to understand the concept well.
import math inp=5 sup = 1 n = inp for i in range(inp): term = math.pow(i+1, n) sup = sup * term n = n - 1 print(sup)
Thank you. Hope it was helpful.