Hệ số nhị thức Python
Trong hướng dẫn này, chúng ta sẽ xem cách triển khai Định lý nhị thức trong Python và in chuỗi tương ứng cho một tập hợp đầu vào nhất định Show Chúng tôi sử dụng Định lý nhị thức trong việc khai triển phương trình tương tự như (a+b)n. Để mở rộng phương trình đã cho, chúng tôi sử dụng công thức được đưa ra dưới đây
Trong công thức trên,
ở đâu,
Ví dụ (a+b)4 = 4C0a4b0 + 4C1a4-1b1 + 4C2a4-2b2 + 4C3a4-3b3 + 4C4a4-4b4 (a+b)4 = a4 + 4a3b + 6a2b2 + 4ab3 + b4
Thuật toán cho Định lý nhị thức Python
Chúng ta có thể triển khai thêm một hàm bên trong để định dạng các thuật ngữ chứa nhiều mệnh đề if-else. Các điều khoản này sẽ quyết định cách các điều khoản sẽ được định dạng
Mã Python. In chuỗi nhị thứcdef form_series(co_a, co_b, n): """ This method creates the Binomial Theorem Series. :param co_a: coefficient of a :param co_b: coefficient of b :param n: power of the equation :return: None """ def formatting(next_term, coeffs): """ This is an inner function which formats the terms of the binomial series. :param next_term: coefficient of next term :param coeffs: powers of a and b :return: formatted term """ if next_term == 1: coeffs.insert(0, "") else: coeffs.insert(0, next_term) if coeffs[1] == "^0" and coeffs[2] == "^0": return coeffs[0] elif coeffs[1] == "^0": return "{}b{}".format(coeffs[0], coeffs[2]) elif coeffs[2] == "^0": return "{}a{}".format(coeffs[0], coeffs[1]) elif coeffs[1] == "^1" and coeffs[2] == "^1": return "{}ab".format(coeffs[0]) elif coeffs[1] == "^1": return "{}ab{}".format(coeffs[0], coeffs[2]) elif coeffs[2] == "^1": return "a{}b".format(coeffs[0], coeffs[1]) return "{}a{}b{}".format(coeffs[0], coeffs[1], coeffs[2]) # Initializing a list named as `series` series = list() # Calculating the First Term, Formatting it # and Appending it to our Series first_term = pow(co_a, n) coeffs = ["^" + str(n), "^0"] series.append(formatting(first_term, coeffs) + " + ") next_term = first_term # Calculating, Formatting and Appending # the remaining terms. for i in range(1, n + 1): # We can find next term using the # previous term and the formula # mentioned below. next_term = int(next_term * co_b * (n - i + 1) / (i * co_a)) # Pre-formatted list creation coeffs = ["" if x == 1 else "^" + str(x) for x in [n - i, i]] # Append till last term is not reached if i != n: series.append(formatting(next_term, coeffs) + " + ") # Append the last term. else: series.append(formatting(next_term, coeffs)) # Joining the series as a string and printing it. print("".join(series)) if __name__ == "__main__": # Taking inputs print("( a + b ) ^ n") co_a = int(input("Enter the coefficient of a: ")) co_b = int(input("Enter the coefficient of b: ")) n = int(input("Enter n: ")) print("({}a+{}b)^{} = ".format(co_a, co_b, n),end=" ") # Calling the Function form_series(co_a, co_b, n) Đầu vàoĐịnh lý nhị thức Python đầu ra
Cảm ơn bạn đã dành thời gian quý báu của mình để đọc bài viết này. Bạn cũng có thể kiểm tra các bài viết khác Hệ số nhị thức trong Python là gì?Hệ số nhị thức C(n, k) có thể được định nghĩa là hệ số của x^k trong khai triển của (1 + x)^n.
Lệnh cho các hệ số nhị thức là gì?Hệ số nhị thức được ký hiệu là c(n,k) hoặc n cr is defined as coefficient of xk in the binomial expansion of (1+X)n. The Binomial coefficient also gives the value of the number of ways in which k items are chosen from among n objects i.e. k-combinations of n-element set. |