We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 895dffb commit cc5751dCopy full SHA for cc5751d
maths/combinations.py
@@ -1,9 +1,6 @@
1
"""
2
https://en.wikipedia.org/wiki/Combination
3
4
-from math import factorial
5
-
6
7
def combinations(n: int, k: int) -> int:
8
9
Returns the number of different combinations of k length which can
@@ -35,8 +32,11 @@ def combinations(n: int, k: int) -> int:
35
32
# to calculate a factorial of a negative number, which is not possible
36
33
if n < k or k < 0:
37
34
raise ValueError("Please enter positive integers for n and k where n >= k")
38
- return factorial(n) // (factorial(k) * factorial(n - k))
39
+ res = 1
+ for i in range(k):
+ res = res * (n - i)
+ res = res // (i + 1)
+ return res
40
41
if __name__ == "__main__":
42
print(
0 commit comments