我必须编写一个函数,该函数接受一个参数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)