# coding:utf-8
# 秦九韶算式,减少多项式四则运算量
def func(factors, x):
result = factors[0]
for factor in factors[1:]:
result = result * x + factor
return result
# x + 1
factors = (1, 1)
print(func(factors, 2))
# ((((3x + 9)x + 0)x + 12)x + 4)x + 6是五次加法,五次乘法
# 3x^5 + 9x^4 + 12x^2 + 4x + 6是15次乘法,五次加法
factors = (3, 9, 0, 12, 4, 6)
print(func(factors, 5))
输出:
3
15326
# 一句话
def func(factors, x):
return reduce(lambda a, b: a * x + b, factors)
秦九韶算法解析
本文介绍了一种基于秦九韶算法实现的多项式求值方法,该方法通过递推方式减少了多项式的运算次数,提高了计算效率。举例展示了如何用Python语言实现此算法,并给出具体的运行结果。
6894

被折叠的 条评论
为什么被折叠?



