MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / choose

Function choose

project_euler/problem_113/sol1.py:21–34  ·  view source on GitHub ↗

Calculate the binomial coefficient c(n,r) using the multiplicative formula. >>> choose(4,2) 6 >>> choose(5,3) 10 >>> choose(20,6) 38760

(n: int, r: int)

Source from the content-addressed store, hash-verified

19
20
21def choose(n: int, r: int) -> int:
22 """
23 Calculate the binomial coefficient c(n,r) using the multiplicative formula.
24 >>> choose(4,2)
25 6
26 >>> choose(5,3)
27 10
28 >>> choose(20,6)
29 38760
30 """
31 ret = 1.0
32 for i in range(1, r + 1):
33 ret *= (n + 1 - i) / i
34 return round(ret)
35
36
37def non_bouncy_exact(n: int) -> int:

Callers 1

non_bouncy_exactFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected