Skip to content

Instantly share code, notes, and snippets.

@nicholaskajoh
Created November 9, 2016 13:33
Show Gist options
  • Save nicholaskajoh/87409bca2e6eff2ad165d9704937183d to your computer and use it in GitHub Desktop.
Save nicholaskajoh/87409bca2e6eff2ad165d9704937183d to your computer and use it in GitHub Desktop.
# binomial coeficient
import math
if __name__ == "__main__":
print("#### Binomial Coefficent Calculator ####")
print("Use: Given (a + b)^n, p = coefficient of a, q = coefficient of b, t = nth term")
# get params n, p, q and t
n = int(input("n: "))
p = int(input("p: "))
q = int(input("q: "))
t = int(input("t: "))
# solve
k = t-1
C = math.factorial(n) / (math.factorial(n-k) * math.factorial(k))
a = p**(n-k)
b = q**k
print("Coefficient: ", C*a*b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment