The Gateway to Computer Science Excellence

+65 votes

Best answer

0

to get 6 as answer you will require more than 1 temporary variavle, but in qsn it has mentioned tha only 1 temp variable is available.

+4

i think i get it

x((x^{2}((x^{2})+4))+6)+5 // x^{2} is done 1st

then x((x^{2}((x^{2})+4))+6)+5 now we lost x^{2}

so need to do x^{2} again

is it correct

+6

see, if you even replace x=x^{2}, you have to first store that x in some temp, because x is used in outside last bracket **x**((x^{2}((x^{2})+4))+6)+5 ,

the answer would be 6, if in place of last **x**, it would be **x ^{2}**

try to write in a form of 3 addr code, you will understand what is happening.

+1

Given value of X, using only one temporary variable

Hi @Anu007 ji, I think what ever you are saying looks correct to me.

Answer could be 7 if nothing is given. But explicitly they are saying one temporary variable which means we can store $X^2$ value and avoid recomputation.

ping @kenzou, @joshi_nitish

0

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.

+28

This will be the code according to answer ...

1. T= x

2. T = T * T [$x^{2}$]

3. T = T + 4 [$x^{2}$ + 4 ]

4. T = T *x [$x^{3} + 4x$]

5. T = T * x [$x^{4} + 4x^{2}$]

6. T = T + 6 [$x^{4} + 4x^{2}+ 6$]

7. T = T * x [$x^{5} + 4x^{3}+ 6x$]

8. T = T + 5 [ $x^{5} + 4x^{3}+ 6x +5$ ]

No of operation = 7

We hav to evaluate that expression using * single temporary variable * .....

0

@Anu007 Yes the answer should be 6. We can take a temporary variable and store 'x^{2' }in it, then this can be done in 6 operations only

+20 votes

Multiplications, 3 Additions]

So answer is 7 arithmetic operations.

+9 votes

^{5}+4x^{3}+6x+5 = x^{3}(x^{2}+4)+6x+5

x^{3} -> 3 multiply operations (also gives x^{2})

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

+6 votes

P(X) = X^{5}+4X^{3}+6X+5

RHS

X(X^{4}+4X^{2}+6)+5

X(((X^{2})^{2}+2.2.X^{2}+2^{2})+2)+5

X((X^{2}+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

0

This is wrong. (X^{2})^{2}+2.2.X^{2}+2^{2 }

cannot be equal to (X^{2}+2)^{2}

It's 2.2.X^2 in middle term..not 2.2.X

0

@Divya

Here, a=X^{2 }, b=2, thus middle term is 2.2.X^{2}. If you see carefully, it is correct. I think you have overlooked the power 2.

0

Yeah, I've read all the comments already. The selected answer assumes that $x, 4, 6, 5$ are given, and we are given a temporary variable $T$ to work with. Because after doing $x(x) +4$ the result will be in temporary variable and now to get $x(x(x) + 4)$ we definitely need to know $x$. So, there's no way that $x$ IS the temporary variable.

Now, as pointed out in the comments, particularly one which describes the three address code for the selected answer, directly references $x$ in the 4th line.

This answer also, doesn't need to store $x^2$ in the variable cause it is being used only once, and $x$ is readily available, this answer seems correct.

Correct me if I'm wrong. Sorry for ping.

- All categories
- General Aptitude 1.9k
- Engineering Mathematics 7.5k
- Digital Logic 2.9k
- Programming and DS 4.9k
- Algorithms 4.3k
- Theory of Computation 6.2k
- Compiler Design 2.1k
- Databases 4.1k
- CO and Architecture 3.4k
- Computer Networks 4.1k
- Non GATE 1.5k
- Others 1.5k
- Admissions 595
- Exam Queries 576
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 17

50,647 questions

56,503 answers

195,502 comments

100,866 users