Skip to content

Instantly share code, notes, and snippets.

@jedrzejme
Last active May 26, 2023 13:43
Show Gist options
  • Save jedrzejme/4f52b0ace1a6c14591b9a14828bb5b9b to your computer and use it in GitHub Desktop.
Save jedrzejme/4f52b0ace1a6c14591b9a14828bb5b9b to your computer and use it in GitHub Desktop.
Combinations calculator in Python
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