- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hello! (Wave)
The following part of code implements the Horner's method for the valuation of a polynomial.
$$q(x)=\sum_{i=0}^m a_i x^i=a_0+x(a_1+x(a_2+ \dots + x(a_{m-1}+xa_m) \dots ))$$
where the coefficients $a_0, a_1, \dots , a_m$ and a value of $x$ are given:
I thought the following:
At the line [m]1[/m], [m] 1 [/m] command is executed.
At the line [m]2[/m], [m] 1 [/m] command is executed.
The line $3$ is executed [m] m+1 [/m] times.
The line [m]4[/m] is executed [m] 3m [/m] times.
The line [m]5[/m] is executed [m] 2m [/m] times.
So, the time complexity of the part of the code is $O(m)$.
Is it right? How could I formulate it better? (Thinking)
Or have I understood it wrong?
The following part of code implements the Horner's method for the valuation of a polynomial.
$$q(x)=\sum_{i=0}^m a_i x^i=a_0+x(a_1+x(a_2+ \dots + x(a_{m-1}+xa_m) \dots ))$$
where the coefficients $a_0, a_1, \dots , a_m$ and a value of $x$ are given:
Code:
1.k<-0
2.j<-m
3.while (j>=0)
4. k<-a_j+x*k
5. j<-j-1
- Which is the asymptotic time complexity of this part of the code for the Horner's method?
I thought the following:
At the line [m]1[/m], [m] 1 [/m] command is executed.
At the line [m]2[/m], [m] 1 [/m] command is executed.
The line $3$ is executed [m] m+1 [/m] times.
The line [m]4[/m] is executed [m] 3m [/m] times.
The line [m]5[/m] is executed [m] 2m [/m] times.
So, the time complexity of the part of the code is $O(m)$.
Is it right? How could I formulate it better? (Thinking)
- Write a pseudocode for the implementation of the simple algorithm of valuation of a polynomial, that calculates each term of the polynomial independenlty from the others.
Which is the time complexity?
Compare it with the correspong time complexity for the Horner's method.
Code:
k<-0
l<-0
i<-m
while j>=0
l=pow(x,j)*a_j;
k+=l;
j-=1;