[英]integers numbers Roots of Polynomials in python
我必須編寫一個 function,它接受一個參數 p,其列表表示多項式 p(x),並返回 p(x) 的整個根的規范列表。 例如,系數為 [1, -5, 6] 的多項式必須返回根 [2, 3]。 我必須使用列表並考慮第一個系數將始終為 1。
degree = int(input('degree: '))
p=[]
i=0
while i <= degree: #creates polynomial
coef = int(input("Coef: "))
p.append(coef)
i+=1
# creates p(possible roots)
pê=[0]
for x in range(1,p[-1]):
if b % x == 0:
pê.append(-x)
pê.append(x)
for g in pê:
您可以為此使用 numpy。
import numpy as np
coeff=[1,-5,6]
roots=np.roots(coeff)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.