Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Permutations_Combinations_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from math import factorial # Import the factorial function from the math module.

# Function to calculate permutations (n choose r).
def permutations(n, r):
# Use the factorial function to calculate the factorial of n and (n-r).
numerator = factorial(n)
denominator = factorial(n - r)

# Calculate and return the result as the ratio of the factorials.
result = numerator // denominator
return result

# Function to calculate combinations (n choose r).
def combinations(n, r):
# Use the factorial function to calculate the factorials of n, r, and (n-r).
numerator = factorial(n)
denominator = factorial(r) * factorial(n - r)

# Calculate and return the result as the ratio of the factorials.
result = numerator // denominator
return result