Skip to content

Commit e88d173

Browse files
authored
Create nCr.py
Calculate nCr using fermits little theorem
1 parent ce871fc commit e88d173

File tree

1 file changed

+24
-0
lines changed
  • math/nCr_computations/python

1 file changed

+24
-0
lines changed

math/nCr_computations/python/nCr.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
3+
fermits little theorem
4+
nCr % p = (fac[n]* modinverse(fac[r]) % p * modinverse(fac[n-r]) % p) % p
5+
6+
modinverse(x) % p = x^p-2
7+
8+
'''
9+
10+
11+
def ncr(n, r, p):
12+
numerator = denominator = 1
13+
for i in range(r):
14+
numerator = (numerator * (n - i)) % p
15+
denominator = (denominator * (i + 1)) % p
16+
return (numerator * pow(denominator , p - 2 , p)) % p
17+
18+
19+
n, r, p = 10**5, 20000, 1000000007
20+
21+
print(ncr(n, r, p))
22+
23+
24+

0 commit comments

Comments
 (0)