# Polynomial evaluation (Horner rule)
# Input: A [a0, a1, a2 ... an], the value of x
# Output: value given x p polynomials
# Horner iteration realized in the form
1 # In this modified initial value 2 A = [2,. 6, 15, -5, 34 is ] . 3 X = 2 . 4 # main routine . 5 P = A [-1] # index designated -1, allows to return Python the last list element . 6 for I in Range (. 1 , len (a)): . 7 P P = a * X + [-l- I] . 8 Print ( ' iterative method, the polynomial is: ' , P)
# Horner achieve recursive form
# In this modified initial values A = [2,. 6, 15, -5, 34 is ] X = 2 # main P = A [-1 ] i = 1 def horner(A,x,p,i): p = p*x + A[-1-i] if i < len(A)-1: Horner (A, X, P, I + 1'd ) the else : Print ( ' recursive method, the polynomial is: ' , P) # function call horner (A, x, p, i)
operation result:
Iterative method, the polynomial is: 578
recursive method, the polynomial is: 578