in DS edited by
493 views
7 votes
7 votes

Consider a stack whose elements are unsigned integers and support the following operations:

  • PUSH a: Pushes the element 'a' onto the stack.
  • ADD: Adds the two topmost elements, removes them, and pushes the result.
  • SQR: Computes the square of the topmost element, removes it, and pushes the result.
  • REV: Reverses the order of the three topmost elements in the stack (if the stack contains fewer than three elements, REV is undefined).

Computation starts with an empty stack, and after a sequence of operations, the topmost element is considered the final result.
Given three unsigned integers $x, y$, and $z$, determine the number of instructions needed in the sequence to compute the following expression:
$$
(x+y)^2+z^2
$$
The instruction sequence should start with$: \textsf{PUSH x; PUSH y; PUSH z;} \ldots$
 

No further PUSH operations are allowed. After the entire sequence of instructions executes, the top of the stack should contain the given expression.

If an instruction is used more than once, count it as many times as it is used. Your final count should include initial 3 PUSH instructions.

in DS edited by
493 views

2 Comments

Push(X), Push(Y), Push(Z), SQR, REV, ADD, SQR, ADD so it's only taking 8 operations and the required task is completed right??
3
3
Why first sqr is needed?rev,add,sqr,add will also do ,right??
0
0

1 Answer

5 votes
5 votes
PUSH x; PUSH y; PUSH z; SQR; REV; ADD; SQR; ADD;
edited by

4 Comments

What is SFT?
0
0
last square is not required as equation already in 8th step
0
0
yes 8 is correct answer, it has been updated now.
1
1
Please give stack representation also.
0
0
Answer:

Related questions