 We will continue our discussion on restricting basic Turing machine models in some natural fashions and in fact, we will find all such restrictions do not really cut down the power of recognition. So, today we shall we will consider counter machines counter machines we can represent pictorially in this fashion that it has read only input tape and this is the finite control. So, this finite control the machine itself can be in a number of finitely many states. Now, what it has besides of course, these two a number of counters these are counters. So, this machine has k counters these are counters and a counter is a counter can store any non negative integer. Therefore, it can store 0 it can store any other non negative integers and the kinds of operations allowed on a counter are this check if a counter contains 0 value that is the number which is stored in the counter. You can check whether it is 0 or not if it is not 0 of course, it is going to be something positive increment counter value by 1 decrement counter value by 1 provided this decrement you can do provided counter value stored before the decrement is positive counter value was positive. So, once more a counter is something which can store any non negative integer 0 1 2 3 we have no bound on the size of the integer it can store it what this machine can do with a counter is that it can check if a counter contains 0 value or not. So, in other words what it cannot check or what it cannot make any decision on the particular non 0 value of a counter all it can check is the value 0 or not if it is not it can be anything 1 2 whatever that might be it can always increment a counter value by 1 it can decrement a counter value by 1 that is subtract 1 from the counter value provided the counter value was positive to begin with because you see if it was 0 essentially that means we cannot decrement otherwise we would have got a negative number of course, negative numbers of things which counters cannot store. Now a counter machine therefore, will in a particular state what it will it is next tape will depend on of course, it will depend on its present state input symbols being read. So, that is the symbol under the read only input tape and because it is read only this tape head keeps moving only to the right it can never come back in the sense it of course it is not because it is just read only, but read only one way input tape and that is the kind of convention we had for input tapes for PDAs even stack machines. So, that is the standard restriction that we have read only input tape where the head moves only from left to right and so the present symbol of course, it knows at any given time it is in some particular state let us say q reading an input symbol a and now it can check whether these any one of these or all of these 0 or not. So, essentially the information for C 1 to C k it can check is 0 or non 0 for example, if they are all 0 maybe the next take will be something let us say p and then it should say what are the operations done on C 1 to C k. The operation would be one of these basically either increment by one or decrement by one. So, once more a move of counter machine we can describe by something like this. So, just for the sake of example, let us say we have three counters. So, that move we can say the present state the input symbol being read and let us say we can say here positive 0 positive. What we mean is suppose we had three counters and C 1 is positive that is contains a number more than 0 C 2 is exactly 0 and C 3 is a number which is positive then depending on if this is the situation then you might say increment first of all go to next state is going to be 3 and and operations on C 1 C 2 C 3. So, you can say maybe increment C 1 right increment C 2 and decrement C 3. So, a typical move of a counter machine in this case it is a three counter machine for this example can be described in this manner that depending on the present state and the input symbol that is being read and depending on the nature of values of the three counters and that nature is whether their positive or 0 depending on this information the machine determines what it is next state going to be and what operations to perform on each one of the counters. So, it looks therefore, is a fairly restricted model compared to our basic model in the sense in our basic Turing machine model we had a tape we could have gone both ways we could have written anything so on and here whatever operations we are performing they are very restricted in nature at the same time it turns out that you know a very few counters will be necessary to capture the full power of our basic Turing machine. So, first of all one should realize that a counter can be thought of as a restricted version of a stack why because imagine a stack which has other than the bottom of stack symbol can contain only one some particular symbol as its stack symbol a counter can be seen as a restricted version of a stack and this restricted stack other than the bottom symbol bottom of the stack symbol which is a special symbol which will not occur anywhere else in the stack except here the symbol that it can contain is exactly one symbol that is for example, here only x can occur and x can occur in a number of times. So, you see such a stack you can think of it as a counter because you can check whether the corresponding counter has zero value or not that will be the case when there are no excess only the bottom of the stack marker is there. So, if you are looking at the stack and accessing the top stack element and if the top stack element happens to be this symbol then you know the corresponding stack corresponding counter contains the value zero increment counter value by one which just means push a symbol x decrement counter value one by one provided counter value as positive it of course means that if you are seeing an x as the top of the stack symbol you can pop it. Therefore, you can see that counter any counter can be modeled as a stack of a very restricted kind and once we see this we immediately know that one counter machine is a restricted push down automata. By the way we will assume that our counter machines that we are considering they are deterministic and even then ultimately what we are going to show is that with just two counters we can simulate during machine. Now, the point is a one counter is of course very restricted especially because it is also deterministic. So, it is a deterministic PDA and that to the stack can contain only symbols in this manner. So, it is a very very restricted kind of a machine. Let us now consider the case counter machines with three counter. See our strategy is first of all we show that three counter machines can simulate during machines and then we will show that just by using two counters you can also simulate during machines. And the reason for not going directly to three counter machine two counter machines is because the idea of the first proof that is sufficiency of three counters will be required you know for the proof of two counter. In fact, we will start with a three counter machine simulating a during machine and we will see how we can do with only two counters later on alright. So, what we are trying to prove now that two counters suffice this is what we are trying to prove first of all not two the case of two, but three counters suffice. Suffice in which sense suffice in the sense that with three counters a counter machine with three counters can simulate during machine right. So, our idea is going to be that you give me a during machine and I will give you a counter machine with three counters which will accept the same language as that of the during machine. The way we will do this is through the intermediate result we proved earlier that two stack machines a stack machine with two stacks can simulate during machine. So, what we will actually show will be that three count of counter machine with three counter can in general can simulate two stack stack machines and the strategy of this proof will be how do we show this that will essentially what we are going to do that will have one counter to represent one stack another counter to represent the second stack and the third counter we shall use to do the manipulation stack manipulations on these two stacks. So, strategy for demonstrating the truth of the sentence is as I said that we represent each of the two stacks a counter by one counter by basically what we mean is. So, here is we have a stack machine recall a stack machine looks like that it has some input tape and it has two stacks. So, this is stack one and this is stack two stack machine. So, you give me such a machine what we are going to do is to consider a particular counter machine which has just two counters this is C 1 and C 2 and will what we will do is C 1 to represent stack 1 and C 2 to represent stack 2 how do we make this representation. So, that you know all our operations will be conveniently done by counter machine operations the stack operations to be simulated by counter machine operations what you can think of suppose we have a stack and we had some symbols appearing in the stack and of course. So, here there are five symbols now imagine the number five four one three two one. So, what is what I am doing I am each stack symbol I am think of thinking of the stack symbol as some x i and. So, let us say there are x 1 through x k these are the stack symbols. So, there are k stack symbols in general one x 1 x 2 after all then the contents of a stack is a sequence finite sequence of with symbols from this set and now I can think of this as one this as two and this as k in a k plus one a re-representation. So, these are the digits this is digit one digit two digit k of a k plus one a re-representation of numbers and then I from top to bottom we write the k re-representation number in left to right fashion. So, this example is clear that suppose x 5 x 4 x 1 x 3 x 2 x 1 that is how the stack appeared at some point the corresponding number that I am talking of in this case the base is six because you know let us say there are these five symbols can appear we have some number and as I said that we write the number in this form well here actually I should have said that this when I write like this I would like to see it as the least significant digit and this as the most significant digit this is of course that is how we read the stack I am writing like this, but imagine the most significant digit of the corresponding number which this codes is the number which is at the bottom of the stack here and so on. So, now think of so therefore, this is a definite number and what is that number in this case when the representation is six sari. So, you can see this is going to be this is the 0th first second third fourth fifth right. So, I can think of this as 6 to the power 5 1 into 6 to the power 5 plus 2 into 6 to the power 4 plus 3 into 6 to the power 3 plus 1 into 6 to the power 2 plus 4 into 6 to the power 1 plus 5 right. So, this is a very simple idea the idea is that think of the string which is there on the stack at any time as a representation of a certain number and this number the most significant digit is appearing at the bottom and the least significant digit is appearing at the top all right. So, now this, but this is a particular number right and numbers are things which we can and this of course, as you can see this is going to be a positive number always representation of a stack contents in this manner is going to be a positive number or rather I should say non negative number. And what we can do is to say that this number is what is stored in a counter empty the stack empty means the number is 0 you can put it that way. So, now you see suppose you wanted to pop this number or rather this is a stack. So, you wanted to pop this symbol. So, then once you pop this symbol the stack would have had x 1 x 2 x 3 x 1 x 4 once you have popped the top symbol the stack will look like this. But you can easily see what is now the number that corresponds to this contents of the stack right. So, the top of the stack symbol was represented by the last or the least significant digit. So, essentially what we are doing is if you want the number corresponding to this you are supposing this original number is n all you are doing is n div k or k plus 1 we are seeing our arities in this in general it is a k plus 1 array representation. Now, in this particular case the number that I will get here since it is a 6 array representation is n div 6 right. So, this is equivalent to the number n div 6. So, obtaining the resultant stack after popping the top symbol is equivalently if you think in terms of counters is obtaining the making the integer division by a certain fixed number 6 in this case. Now, how can you carry out this in terms of a counter remember that suppose this was your stack 1 and therefore this number n was stored in counter c 1 counter c 2 would store the number corresponding to to the stack contents of the second stack and recall that we are considering 3's counter machine. So, we had a pair counter what I will do. So, this is the pair counter or auxiliary counter I will use this to find out the result and that is fairly simple is not it what we are doing supposing we had n here. So, we start decrementing n every time we decrement 6 here we add 1 to this starting of course with the value 0 is not it. So, what will happen suppose the number was let us take a simplistic case 33. So, first time you reduced this by 6 so how you why what is that 6 because that is a fixed thing that is the addity which you are using for representing numbers which is. So, therefore you know and 6 is a constant so far as one particular stack machine is concerned because that the number of different symbols in stacks will be a constant and that is the constant which is going to define your addity of number representation. So, decrement 6 times this is possible 6 or whatever that constant thing is now that since it is a constant we can do this decrement by means of a finite number of steps that is of course you want 6. So, 6 steps that will require of the counter machine 6 decrements here and once you complete all 6 without this number becoming 0 that is the numbers result you could fully take out 6 from here then you will add 1 here right. So, at that time so basically you subtracted 6 so what would you get you will get 27 having decremented 6 from here. So, once this value became 27 this value became 1 then next time another round of taking out 6 from here you will get 21 here it will be incremented by 1 so this value this counter value c 3 will be 2 and so on right. So, at some point of time you will have 3 left and this counter value will be 5 and now when you are trying to decrement 6 from here you have done 1 you have done 2 you have done 3 and then immediately we will see this particular counter has become 0. So, therefore, you know also in this process what is n mod e plus 1 in general or in our particular example what is n mod e plus mod 6. So, this would give you what is the top stack symbol which you have just popped and this will give you the value of the counter that would correspond to the stack which results after popping of the top symbol. So, what I am trying to say and this is now fairly clear that popping operation in a popping operation of a stack we get something the top stack symbol to do whatever we wish to do with it and then the stack becomes less by 1 symbol. So, both these operations we can do by in this simple manner. Now of course, you after getting the right value in C 3 here will copy back C 3 contents to C 1 because that would have been what is desired because C 1 is representing stack 1. So, any particular stack operation whether it is C 1 or C 2 so long it is a pop operation popping of the stack we can do it by this manner. It is very easy to see that pushing or adding a symbol push on a stack also can be similarly carried out. So, if you wanted to push let us say 4 on this right this was the number and you wanted to add 4 to it. That means correspondingly the number the symbol x 4 is pushed on top of the stack in the corresponding number would have been this now this has become the this 4 has become the less significant digit. How can you get the resultant number the resultant number would have been simply n into 6 plus 4 right because remember this side is the least significant digit and this side is the most significant digit. And can we carry out such an arithmetic operation using the counters only using the counter operations yes that also we can why because let us say I have a counter which is currently contains n and I would like from here finally the counter to contain let us say 6 n plus 4. Now remember we will do this using the auxiliary counter and what will be the strategy here we decrement one from here and add 6 to this auxiliary counter every time I add decrement one here I add 6. So, if I do so when this becomes 0 this has 6 n as contains auxiliary counter. So, auxiliary counter will have 6 n and now this is a definite constant that you are going to add this because of whatever reason because you know in some step x 4 was added on top of the stack. So, now you add 4 to this that means increment 4 times we will get 6 n plus 4. So, now the auxiliary counter contains the correct value to represent the new stack here the stack contains here. So, now you copy back the contents of here to here by decrementing one incrementing one decrementing one incrementing one remember it had already gone 0 and so on. Therefore, if you just perform this operation in decrement this increment this still this becomes 0 will transfer the contents of auxiliary counter to the counter of that corresponded to the stack whose operation push operation we were simulating. And therefore, my point is that both push and pop we just saw that push on a stack we can simulate by counter machines in fact just by using two counters we can simulate the push of a stack what not exactly push of any string, but what we had shown was push of a symbol one symbol, but that is general enough. Because you can always even if your p d a wanted to push a string which is whose length is more than one that is it contain more than one symbol this you can of course think of as pushing separately all these symbols in the string. So, this is general enough therefore, what I have just told you to summarize it will mean three counter machines can once represent two stacks by two counter second what is the other thing it can do that can simulate push and pop with the help of the auxiliary counter. Now what else is left in for simulating a two stack machine see a two stack machine of course reads symbols from input, but that is direct is not it because your three counter machine will also have an input tape and as it reads an input here the same input can be read the same input symbol can be read from the three counter machines corresponding input tape. The stack machine changes states with the state of the stack machine we can keep track of in the finite control of the three counter machine. So, therefore, it is not difficult to conclude from the discussion that three counter machines can tip by two counter machines by step simulate the operations of a two stack machine. So, therefore, our conclusion is since three counter machines can simulate two stack machines and since two stack machines can simulate during machines we conclude that such machines that is three stack machines three counter machines can simulate you see it is a fairly strong restriction instead of a tape of a two ring machine the memory is now simulated memory component of an algorithm is being simulated by just three counter. Now the interesting thing is that actually we can do only with two counter now let us prove this statement the way will prove this is that will show that three counter machines can be simulated by two counter machine proof idea consider a specific three counter machine. So, it is let us say it is three counters are C 1 C 2 and C 3 these are the three counter now our two counter machine what it is it going to do that it will represent all these three counters in one single counter. So, let us say C 1 dash and C 2 dash are the counters of the corresponding two counter machine and here what will do is first of all let us say that will say that C 1 dash will represent or will have all the information of all these three stacks. Now recall that these three stacks at any given time it contains three numbers N 1 N 2 and N 3. So, essentially keeping track of three counters is keeping track of a three tuple of numbers N 1 N 2 N 3 any one of which could be zero, but none is negative. So, that is a very simple way of there is a very simple way of representing a three tuple of numbers by means of one number that is well known and this is called Godel numbering. So, consider this number two to the power N 1 three to the power N 2 and five to the power N 3. Notice that these are two three and five are prime numbers and this therefore is a is one definite number N and I claim this N uniquely represents N 1 N 2 N 3. So, the number of twos in this number N the first number of factors basically two factors is N 1 number of threes as factors is N 2 number of fives as factors of N will be N 3. Now counter machines will of can perform certain operations and those operations are check if a counter is zero right these are the operation increment a counter and decrement positive counter value by one these are all by one all right. Now, let us say we want to check if C 2 is 0 the counter value C 2 is zero that means what when C 2 is 0 that means N 2 is 0 the number N has no counter value three as a factor in such a case right C 2 is 0 if and only if N is not divisible by three. So, can I check if the number is divisible by three or not yes by the simple use of the auxiliary counter remember I have now two counters in this two counter machines one representing the values N 1 N 2 N 3 in one go with such a number. So, I have this number N and I want to check if this number N is divisible by three what do I do I can keep taking out keep decrementing in this counter in groups of three. If I when I reach zero by then if I have completely taken out another bunch of three then I know this number is divisible by three right. So, for example, seven if I first decremented by make three decrements I will get four then I make three decrements I will get one and now this one I cannot as I try to decrement three times I will find that I will not be successful and the result will be the counter value will become zero before the you know we have managed to decrement three times. So, I know the number is not divisible by three and since it is not divisible by three therefore, C 2 is definitely zero actually we are not losing out anything because as you are decrementing three at a time you can keep putting the number of times you have been it has been possible for you to decrement three times I mean one one one and you know basically the result of N div three can be here and on hand you will know how many the remainder will be there. So, you can actually restore back this number. So, that is also nothing is getting lost in doing this checking increment a counter by one. So, let us see you wanted to increment this counter C 3 that means just multiplying the number by five. So, increment I can do decrement a positive counter by one is dividing the same thing essentially if I supposing I wanted to decrement this counter provided it is N 1 is non-zero and that is possible all I need to do is to divide the number get the number N div 2 because when you decrement N 1 you will have two here N 1 minus 1 and the old value old this new N prime is just old value of N divided by two and we have already seen how we can divide by a fixed constant remember these are fixed constants 2 3 and 5. You should note that this you know situation will not be possible if instead of prime numbers we had put something else for example, instead of five had you put four and for the representation so your representation would have been 2 N 1 3 N 2 4 N 3 and now we said that divisibility by two will tell me whether N 1 is 0 or not. So, suppose N 1 is 0 and yet the number will be divisible by two so that is why you need prime numbers whose you know powers are used to keep track of the contents. Now, to summarize what we have said is that the operation of three counters can be simulated by just two counters all the three counters of the original three counter machine their contents are represented in this manner in a composite manner in one counter and the auxiliary counter is used to do operations on the three counters to simulate the operations of the three counters we can do checking by zero increment decrement. So, therefore, the just with two counters we can simulate all the three counters therefore the two counter machine we can design which will be doing same thing as any given three counter machine and therefore, we say two counters will suffice. In conclusion, Turing machines can be simulated by counter machines with only two counters please remember that if you cut down the number of counters by one if you had one counter machine then we will have a very restricted form of a DPDA which is of course, cannot in general simulate Turing machine.