The conventional algorithm for evaluating a polynomial anxn + an-1xn-1 + … + a1x + a0 at x = c can be expressed in pseudocode by

Procedure polynomial (c, a0, a1, …, an : real numbers)
power :=1
y :=a0
for i := 1 to n
begin
power := power * c
y := y + ai * power
end {y = ancn + an-1cn-1 +…+ a1c +a0}
where the final value of y is the value of the polynomial at x = c.

a. Evaluate 3x2 + x + 1 at x = 2 by working through each step of the algorithm showing the values assigned at each assignment step.
b. Exactly how many multiplications and additions are used to evaluate a polynomial of degree n at x = c? (Do not count additions used to increment the loop variable.)

Respuesta :

Answer:

a.) value of polynomial at x=2 is 15

b,) exactly 2 multiplications and 1 addition are used to evaluate the polynomial

Step-by-step explanation:

a.)

at x = 2,

3x²+x+1

with n = 2, a₀=1, a₁=1, a₂=3 with c=2,

firstly we set power =1

y=1

for the first time through the loop;

we have i = 1

the power increases to 2

y = 1+1*2

= 3

i =2

then going through the loop for the last time

power = 2x2 = 4

y = 3+(3*4)

= 15

we stop here because polynomial is of degree 2

b.

while passing through the loop, each pass requires that we do 2 multiplications and 1 addition. so for the for loop we have n iterations with total being  2n multiplications and n additions.