Last active
May 26, 2023 13:43
-
-
Save jedrzejme/4f52b0ace1a6c14591b9a14828bb5b9b to your computer and use it in GitHub Desktop.
Combinations calculator in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def combinations(n,k): | |
def factorial(n): | |
fact = 1 | |
for i in range(1, n+1): | |
fact = fact * i | |
return(fact) | |
result = int(factorial(n) / (factorial(k) * factorial(n-k))) | |
return(f"{result:,}".replace(",", " ")) | |
# 1st example of usage: | |
print(combinations(int(input("How many elements in the set? ")), int(input("How many elements in the subset? ")))) | |
# 2nd example of usage: | |
print(combinations(62, 5)) | |
# 3rd example of usage: | |
n = 62 | |
k = 5 | |
print(combinations(n, k)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment