in Compiler Design
19,173 views
60 votes
60 votes
The minimum number of arithmetic operations required to evaluate the polynomial $P(X) = X^5+4X^3+6X+5$ for a given value of $X$, using only one temporary variable is ______.
in Compiler Design
19.2k views

7 Answers

121 votes
121 votes
Best answer
$P(X) = x^5 + 4x^3 + 6x + 5$

$=x(x^4 + 4x^2 + 6) +5$

$=x( x ( x^3 + 4x) + 6) + 5$

$=x( x ( x (x^2 + 4)) + 6) + 5$

$=x( x ( x (x(x) + 4)) + 6) + 5$

mul = pair of brackets $4$
add = num of signs $3$
total $7$
edited by

4 Comments

@Abhrajyoti00    regarding your first line, I think it will be

T ← x

T ← T * x

0
0

@Alekhyo Banerjee Why? It is not mentioned in the question that “arithmetic operations have to be done after storing them somewhere”?

They just asked min no. of arithmetic operations reqd using 1 temp variable. So why should I store $x$ first then do $x^2$? We do like this if they ask : “min register reqd. or if it’s explicitly mentioned that operations can only be performed in integer

0
0
is the answer will change if number of temp variable is $\geq1$.
0
0
25 votes
25 votes

In the above 8 steps of evaluation, the total number of arithmetic operations required are 7 [4
Multiplications, 3 Additions]
So answer is 7 arithmetic operations.

2 Comments

If one could store in memory, it doesn't make sense to mention one temporary variable.Storing in memory is equivalent to having infinite temporaries.One could get answer 6 if storing in memory is allowed.
1
1
can u explain 2nd and 3rd line ??? Temporary value T is getting lost during that time ...
0
0
17 votes
17 votes

x5+4x3​+6x+5 = x3(x2+4)​+6x+5
x3 -> 3 multiply operations (also gives x2)

So, Totally we need 3 + 1 multiplications and 3 additions = 7 operations

by

1 comment

@Arjun sir,

That means 3 multiplication for (x^5 part) and one multiplication for (6*x part) right?

3 addition was pretty easy. (that can be guessed intuitively)
0
0
7 votes
7 votes

P(X) = X5+4X3+6X+5

RHS

X(X4+4X2+6)+5

X(((X2)2+2.2.X2+22)+2)+5

X((X2+2)2+2)+5

Now:

T=X*X

T=T+2

T=T*T

T=T+2

T=T*X

T=T+5

Therefore - (* operator used) =3, (+ operator used) = 3 Total=6

4 Comments

If the selected answer can do it, this answer can do it too. So, yes.
0
0

@toxicdesire

It is not possible.

because, I thought like this,can we do one register value multiplied with same register value and stored in same register. Is it possible?

Where selected ans done it?

0
0
@srestha see comments to best answer giving 3 address code …..there at second step same thing has been done

This will be the code according to answer ...

1. T= x

2. T = T * T
0
0
Answer:

Related questions