In this tutorial, you will learn about falling factorial in Python.
You will learn what is a falling factorial in Python.
And how does falling factorial work?
And How you can implement the falling factorial in Python.
So let’s dive into it.
Terms
What is Falling Factorial in Python?
A falling factorial is a mathematical function that takes two arguments, n, and k, and computes the product of k consecutive decreasing positive integers, starting from n down to n - k + 1
For example, you can find the falling factorial of 5 with k=3, denoted as (5)_3, as below:
(5)_3 = 5 * 4 * 3 = 60
In short, a falling factorial is a factorial function that takes in two inputs n and k
The factorial starts from n and instead of ending at one, it ends at k.
You can calculate the normal factorial of 5 as 5*4*3*2*1 which is equal to 120.
But the falling factorial of 5 up to 3 can be calculated as 5*4*3 which is equal to 60.
Falling Factorial Code in Python
You can define a function to compute the falling factorial using a loop that multiplies the value of n by each decreasing integer in the range k.
The function looks like this:
def falling_factorial(n, k):
result = 1
for i in range(k):
result *= n-i
return result
x = falling_factorial(5,3)
print(x)
Click on this code and it will be copied automatically.
Then run the code on our free Online Python Compiler.
Output
60
In this function, we start with the variable result initialized to 1.
Then we loop through the range of k values, multiplying the result by the current value of n-i at each iteration.
Finally, the function returns the value of the result.
When we call this function with arguments (5, 3), it returns the value of 60, which is the falling factorial of 5 with k=3.
Notation & Uses
Uses of the falling factorial
The falling factorial is a mathematical function that you can use to calculate the number of ordered arrangements (permutations) of a certain number of items from a larger set, without repetition.
It is also known as the decreasing factorial or the falling power.
Notation of the falling factorial in Python
The notation for the falling factorial is (n)_k.
Where n is the number of items in the set and k is the number of items in the ordered arrangement.
The value of the falling factorial is calculated as the product of k consecutive decreasing positive integers, starting from n down to n – k + 1.
For example, let’s say we have a set of 6 items.
And we want to calculate the number of ordered arrangements of 4 items.
We can use the falling factorial function to calculate this as:
(6)_4 = 6 * 5 * 4 * 3 = 360
This means that there are 360 possible ways to order 4 items out of a set of 6 without repetition.
Here is the code that illustrates this in Python.
x = falling_factorial(6,4)
print(x)
Output
360
I hope this clarifies the concept of falling factorial in Python.
Let me know if you have any additional questions in the comments below!
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.