Least number of temporary variables required to create three address code in static single assignment form for the expression $p+q*r-s/(q*r)$

I found a question.

Least number of temporary variables required to create three address code in static single assignment form for the expression $$p+q*r-s/(q*r)$$

My attempt:

$$t_1 = q*r$$
$$t_2 = s/t_1$$
$$t_3 = p + t_1$$
$$t_4 = t_3 - t_2$$

So, I got $$4$$ variables but their answer is $$5$$. Am I missing something? Where I am being wrong?

• Looks fine to me. Maybe I'm missing something as well.
– orlp
Dec 18 '18 at 16:23
• Who are "they"? If you can provide an accessible source of question, this question should be simple enough to answer. Dec 19 '18 at 1:59
• @Apass.Jack a local question from an online test series. Dec 19 '18 at 3:20

1 Answer

You used the optimisation of calculating q * r only once. Maybe someone considered that "cheating" and is insisting that it must be calculated twice. I wouldn't agree with that at all, but it would explain the different answers.

• Is it possible the source has committed mistake in its solution? What's the standard procedure? Should optimization not be done while generating $3-$ address code? Dec 19 '18 at 0:13
• It looks like a homework question. There are correct answers, there is your answer, and there’s the answer the teacher expects. Dec 19 '18 at 9:18