温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-27 02:16:54 来源:网络 阅读:1038 作者:浅唱cory 栏目:编程语言

第一种:
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
但是做出来的是第一种时间更短 ,不知道为什么???试了很多次

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI