Python计算一元函数的N次方多项式

第一种:
from datetime import datetime
#f(x)=a0+a1x+a2x*2+a3x**3+...
def f(a,x):

为灌阳等地区用户提供了全套网页设计制作服务,及灌阳网站建设行业解决方案。主营业务为网站建设、成都网站制作、灌阳网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

p=0
for i in range(0,len(a)):
    p=p+a[i]*((x)**i)

print(p)

a=[2,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6]
x=1
begin = datetime.now() # 获取当前datetime
beg_stamp=begin.timestamp()
for i in range(0,100):

t=f(a,x)

end = datetime.now() # 获取当前datetime
end_stamp=end.timestamp() # 把datetime转换为timestamp
print(end_stamp-beg_stamp)

第二种:
from datetime import datetime
#f(x)=a0+x(a1+x(...(an-1+x(an))))
def f(a,x):

p=a[-1]
for i in range(0,len(a)-1):
    b=list(reversed(a))
    p=b[i+1]+x*p

print(p)

a=[2,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6]
x=1
begin = datetime.now() # 获取当前datetime
beg_stamp=begin.timestamp()
for i in range(0,100 ):
t=f(a,x)
end = datetime.now() # 获取当前datetime
end_stamp=end.timestamp() # 把datetime转换为timestamp
print(end_stamp-beg_stamp)

注意:按道理第一种的时间复杂度为n平方,第二种为n
但是做出来的是第一种时间更短 ,不知道为什么???试了很多次


当前标题:Python计算一元函数的N次方多项式
网站链接:http://scjbc.cn/article/pehjop.html

其他资讯