The Theory of Computation

Bernard M.E. Moret

Addison-Wesley-Longman, 1998


Short Solution

Chapter 4, Problem 13




We prove the slightly stronger result that a 2-register machine can simulate a k-register machine.

Note that any general RAM has some fixed number of registers (all those named in the program). We encode all of the registers of the general RAM into the first register of our 2-register machine by using prime encoding: if the general RAM uses k registers and their current values are ri, 1 <= i <= k, the first register or our machine will contain the value product from i=1 to k piri, where pi is the ith prime. The fundamental theorem of arithmetic tells us that such an encoding is one-to-one. The second register of our machine will be used for temporaries. To simulate an increment of register i, our machine simply multiplies its first register by pi; it can do that by first setting the second register to zero, then entering a loop that carries out pi increments of the second register (something that can be done by an ad hoc routine, since pi is a known constant) for one decrement of the first, until the first register is zero, then entering another loop that copies the result from the second back into the first register. To simulate a decrement of register i, our machine divides its first register by pi, which proceeds in the same spirit as the multiplication, except that we carry out pi decrements of the first register for each increment of the second. (Note that the last routine can be modified to test for the presence of a remainder, indicating that the register was 0 before the decrement and should remain unchanged; we can then invert the operation completely to recover the original value.) To test register i for zero, we carry out the division by pi, test whether the remainder is non-zero (indicating there was no factor of pi in the encoding and hence that we had ri=0), then remultiply by pi in order to maintain the original value. Jumps themselves are of course no problem. Thus we have a valid simulation of any given k-register RAM on a 2-register RAM, so that 2-register RAMs (and thus also 3-register RAMs) are universal models of computation.