Question: Using python, solve the following problem: The number of subset s of r elements that can be selected from a set of n elements is
Using python, solve the following problem:
The number of subset s of r elements that can be selected from a set of n elements is written as C(n,r). the value of C(n,r) is also the coefficient of x ^r in the binomial expansion of (x + 1)^n. if r = 0 or r =n, then the value of C(n,r) is 1. otherwise, C(n,r) = C(n -1 ,r - 1). Write a program using a recursive function that allows n to be input by the user and displays the coefficients in the expansion of (x + 1)^n.
the outcome would be:
Enter a int : (5) ##input by user
1 5 10 10 5 1.
Please screenshot the code and output, and do not import other libraries. just create recursion to complete the assignment.
for reference, the follow code can do the same thing but not meet my question requirement
def pascal(n): if n == 1: return [1] else: line = [1] previous_line = pascal(n-1) for i in range(len(previous_line)-1): line.append(previous_line[i] + previous_line[i+1]) line += [1] return line print(pascal(6))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
