From 2779929aca2c222993b2f89d1f6789dddbbb9b82 Mon Sep 17 00:00:00 2001 From: Vinay Reddy Date: Sat, 7 Oct 2023 21:43:10 +0530 Subject: [PATCH] Create Permutations_Combinations_generator.py --- Permutations_Combinations_generator.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Permutations_Combinations_generator.py diff --git a/Permutations_Combinations_generator.py b/Permutations_Combinations_generator.py new file mode 100644 index 0000000..4534d53 --- /dev/null +++ b/Permutations_Combinations_generator.py @@ -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