Suppose there are n + 2 real numbers a0, a1 ,..., The sequence of an, and x must correspond to the polynomial Pn (x) = anx ^ n + a (n-1) x ^ (n-1) +... + A1x + a0 evaluate the value. The direct method is to evaluate each item separately and accumulate the value of each item. This method is very inefficient and requires n + (n-1) +... + 1 = n (n + 1)/2 multiplication and n addition operations. Is there any more efficient algorithm? The answer is yes. Through the following transformation, we can get a much faster algorithm, that is, Pn (x) = anx ^ n + a (n-1) x ^ (n-1) +... + A1x + a0 = ((... (Anx + an-1) x + an-2) x + an-3 )...) X + a1) x + a0, which is called the Horna rule.
All content of this blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/
A = [1,-6, 8, 8, 4,-40]
Myx = 3
B = []
B. append (a [0])
For ai in a [1:]:
B. append (ai + myx * B [-1])
Print B [-1]
Deep @ myddb :~ /Src $ pypy test. py
17
Deep @ myddb :~ /Src $