write a function that returns the value of the binomial expansion for a positive (or zero) integer power n and any valid value of x. first write a function that calculates the value of the rth term. This all has to be done without importing any packages in python. Current code,
def binomial_term_L1(x,n):
return (1+x)**n
but having trouble getting the code for the rth term to work.
def factorial(n):
#function t0 calculate the factorial
#of any value n;
f = 1
for i in range(2, n+1): # loop to calculate the factorial
f *= i
return f
# Function to calculate the rth term of series
def rthterm(X, r , n):
# calculating the value of n!
nFact = factorial(n)
# For calculating the value of nCr
#1.clculating the value of (!r-1)
rFact = factorial(r-1)
#2.calculating the value of (!n-r+1)
nrFact = factorial(n-r+1)
# calculating the value of
# X to the power r-1
xPow = pow(X, r-1)
# return the nCr value
return(int((nFact * xPow)/(nrFact * rFact)))
X = int(input(" enter the value of X : "))
r = int(input(" enter the value of r : "))
n = int(input(" enter the value of n : "))
#calling the function "rthterm" to calculate nCr
k = (rthterm(X,r,n))
#printing the value of nCr
print("The Rth term is : " , k)
Get Answers For Free
Most questions answered within 1 hours.