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)
| 19 | |
| 20 | |
| 21 | def 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 | |
| 37 | def non_bouncy_exact(n: int) -> int: |