Hey as the title says am trying to create a Turing machine that adds 2 2-digit binary numbers.
Up till now I managed to make it work for the case of 10 + 01 but I can't make it work for all the number combinations. Could anyone help? This is my code so far.
The input format is X NUM1 NUM2 X NUM3 NUM4 (x10x01):
State Read Write Direction NextState
0 X X R 0
0 1 1 R 0
0 0 1 L 1
1 X 0 L 1
1 0 0 L 1
1 1 0 L 2
2 X 0 R 2
2 0 0 R 2
2 1 1 R 0
3 _ _ N HALT
You TM does the following:
State 0: move right until we read a 0, then write 1, move left and go to state 1.
State 1: move left until we read a 1, then write 0, move right and go to state 2.
State 2: move right until we read a 1, move right and go to state 0.
State 3 (never reached): Halt
This logic doesn't appear to be adding anything. On input x10x01 it will:
Tape: x10x01, state 0, head at position 1 (x). Move right until we read a 0, write 1, move left and go to state 1.
Tape: x11x01, state 1, head at position 2 (1). Move left until we read a 1, then write 0, move right and go to state 2.
Tape: x01x01, state 2, head at position 3 (1). Move right until we read a 1, move right and go to state 0.
Tape: x01x01, state 0, head at position 4 (x).
Tape: x01x11, state 1, head at position 4 (x).
Tape: x00x11, state 2, head at position 4 (x).
Tape: x00x11, state 0, head at position 6 (1). At this point the machine loops forever or crashes, depending on the specifics of your implementation, as it will read uninitialized tape.
While it might look like the machine added 01 and 10 to get 11, really it just shuffled 1s and 0s around haphazardly.
An actual addition TM would need to do a few more things than your machine is doing. Specifically:
Find the least significant unmarked bit of both operands,
Mark them,
Based on their values, determine the one-bit result and the one-bit carry
Write the result, and apply the carry to the next least-significant unmarked bits.
Repeat until there are no more digits.
Since you're only worried about two-digit numbers, you can skip marking the digits, and just have additional states to track which bit you're working with.
You could also just hard-code all possible inputs (there are only 16), but that's probably not in the spirit of the homework you've been assigned.
For example, to actually add two one-bit numbers, you might want an algorithm like this:
State 0: move right until we read a digit. Write 0. If the digit is 0, go to state 1. If the digit is 1, go to state 2.
State 1: move right until we read a digit. If the digit is 0, go to state 3. If the digit is 4, go to state 4.
State 2: move right until we read a digit. If the digit is 0, go to state 4. If the digit is 5, go to state 5.
State 3: write 0, halt
State 4: write 1, halt
State 5: write 1, move right go to state 3.
In this example, states 1 and 2 track whether the first number was 0 or 1. States 3, 4 and 5 add the numbers together. State 3 is when both are 0. State 4 is when one is 1. State 5 is when both are 1 (this has a carry).
The corresponding TM might look like this:
State Read Write Direction NewState
0 X X R 0
0 0 0 R 1
0 1 0 R 2
1 X X R 1
1 0 0 N 3
1 1 1 N 4
2 X X R 2
2 0 0 N 4
2 1 1 N 5
3 _ 0 N Halt
4 _ 1 N Halt
5 _ 1 R 3
I'll leave it up to you to figure out how to wire two iterations of this concept together to add two 2-bit numbers.
Related
(this is a theoretical question, and therefore it's not related to a particular programming language).
I'm trying to figure out a way to design an algorithm to print a number in binary (i.e. from decimal to binary), the request is easy and it consists of taking the remainders in the opposite order (i.e. remainders of successive divisions).
the first part of the question is easy, for example:
4 / 2 = 2, and 4 % 2 = 0,
2 / 2 = 1, and 2 % 2 = 0,
1 / 2 = 0, and 1 % 2 = 1.
problems comes in the second part: take each digit from the last to the first, I have no clue.
I'm looking for some advice with a query I'm trying to write. I would like to count the occurrence (number of rows) of a column based on whose shift (6-2, 2-10 & 10-6) it falls into. The trouble is the shifts rotate every two weeks, e.g. if I just started on the 6-2 in two weeks time I would be on the 10-6. I have a query that will count said occurrence depending on the shift but I cannot get my head around how to make it count based on whos shift it happened on.
Any advice would be very helpful!
Thanks in advance.
Edit:
Table headings
Serial Number | Date & Time | Part Number | Type | Result
Edit 2 (a bit more detail):
I would like to count how many instances of unknown occur in part number for each our shifts. The part number column contains various numbers (always length 8) identifying the product running through the machine. If the machine for whatever reason doesn't see or read the part number correctly it writes unknown to the table.
e.g.
Week 13: Tom is on 6-2 shift / Alex is on 2-10 shift / Cam is on 10-6
Week 15: Alex is on 6-2 shift / Cam is on 2-10 / Tom is on 10-6 shift
Week 17: Cam is on 6-2 / Tom is on 2-10 shift / Alex is on 10-6 shift.
This pattern continues indefinitely and I want to some the number of unknowns in part number for Cam's shift, Tom's shift and Alex's shift. So I should end up with three numbers. I hope this helps.
OK, so we can get the week number in MySQL. For instance:
WEEK(<Date & Time colum>, 1)
have a look at the link above to decide which mode you need.
There are three shifts and three workers. I'll give them numbers:
Tom = 0
Alex = 1
Cam = 2
6-2 = 0
2-10 = 1
10-6 = 2
I then get this pattern:
week
shift
worker
rotation
0
0
0
0
0
1
1
0
0
2
2
0
1
0
1
1
1
1
2
1
1
2
0
1
2
0
2
2
2
1
0
2
2
2
1
2
3
0
0
3
3
1
1
3
3
2
2
3
4
0
0
0
Now we need to try and compute the 'shift' and 'worker' numbers from the 'week' number. Let's start with the 'shift'. There is no way to get it from week, we just have to define it:
SET shift0 = 0
SET shift1 = 1
SET shift2 = 2
This seems pointless, but bear with me. Now we need to compute the workers. How would we do this, given the 'week' and 'shift'? Here we have to use modulo 3:
SET worker0 = MOD('week' + 'shift0', 3)
SET worker1 = MOD('week' + 'shift1', 3)
SET worker2 = MOD('week' + 'shift2', 3)
Written full out with the WEEK() function:
SET worker0 = MOD(WEEK(<Date & Time colum>, 1) + 0, 3)
SET worker1 = MOD(WEEK(<Date & Time colum>, 1) + 1, 3)
SET worker2 = MOD(WEEK(<Date & Time colum>, 1) + 2, 3)
This is as far as I am willing to take it.
I would try to reconstruct the missing information using the above and store it in the database. Create tables to hold the workers and the shifts. Otherwise you'll have to write very complicated queries, which is not what you want.
I've got a specific problem. My data (map) in mysql is as follows
id table_row table_col tile_type
1 1 1 0
2 2 1 0
3 3 1 0
... ... ... 0
512 512 1 0
513 1 2 0
514 2 2 0
515 3 2 0
... ... ... 0
... 512 2 0
... 1 3 0
... 2 3 0
... 3 3 0
... ... ... 0
... 512 3 0
... 1 4 0
Map is 512*512. I need to come up with an algorithm that selects tiles from the centre(or near centre 256*256) point. So it should look something like
256*256 first - once selected we can update tile_type to 1
255*256 second - update tile_type to 1
256*255 third - update tile_type to 1
257*256 fourth - update tile_type to 1
256*257 fifth - update tile_type to 1
etc. or similar, but it has to start filling in tiles from centre outwards in all directions (can be random). Any ideas appreciated
Your question lacks a few details, but I am assuming you are asking a means of generating an id that is close to the center of your 512x512 grid.
It appears your grid is enumerated in a particular manner: each column is enumerated in increasing order of table_row values, and the enumeration of columns is done in increasing order of table_col values.
Consequently, we can already know the id of the cell for which the table_row and table_col values are 256: it is 255 x 512 + 256. That is correct, because there are 255 full columns that were enumerated before enumeration started for table_col value 256, and each of those columns had 512 rows in them. Finally, within this column, we are interested in row #256.
A more generalized version of this would look like below.
((num_cols + 1) / 2 - 1) * num_rows + (num_rows + 1) / 2
You don't need to care all that much about the +1s and -1s: they are just a numerical hack to handle odd num_rows and num_cols values.
Anyways, to introduce a proximity measure, you can just use two random variables. A random variable P can represent the distance to the center in terms of colums. (i.e. how far the table_col of the point with the generated id will be from the table_col value of the center of the grid) Another random variable Q can represent the distance to the center in terms of rows.
((num_cols + 1) / 2 - 1 + P) * num_rows + ((num_rows + 1) / 2 + Q)
Then you can just generate values for P and Q based on your needs, and get the id of a cell that is P colums and Q rows away from the center of the grid.
Try Below query.
SELECT (MAX(t.`row`+1)/2), (MAX(t.`column`+1)/2) INTO #max_row, #max_col
FROM tiles t;
SELECT t.`row`, t.`column`, ceil(IF(ABS(#max_row - t.`row`) < ABS(#max_col - t.`column`), ABS(#max_col - t.`column`), ABS(#max_row - t.`row`))) as tbl_order
FROM tiles t
ORDER BY 3
I have a weird issue that I cant seem to resolve so hope that converting this to some other form will help:
|coder response|
(coder isBitSet: 1)
ifFalse:[self flagSuccess]
ifTrue:[self flagFailure].
now the issue is coder is a value from 0 to F, when I get a 5 I want it to be treated as 0101, so that it is FALSE from isBitSet:. BUT isBitSet: treats it as 101, so it's always true... so basically isBitSet: isn't working for any binary number thats 4 bits long UNLESS the number is zero...
how can I get my five so that I can check the 4th bit in the number for a 1 or 0?
Try using the bitAt: method. This method extracts a bit from an integer and tells you whether it's a 1 or a 0. It will extract any bit you want and treat bits higher than the size of the integer as 0's.
5 bitAt: 1 ==> 1
5 bitAt: 2 ==> 0
5 bitAt: 3 ==> 1
5 bitAt: 4 ==> 0
5 bitAt: 5 ==> 0
Does that help?
You might not be interpreting the bit numbering correctly. The reason why 5 isBitSet: 1 evaluates to true is that 1 refers to the lowest bit. Regardless of whether 5 is represented as 101, 0101 or even 00101 etc., the lowest bit is always 1 and 5 isBitSet: 1 answers with true.
So in binary to find the largest number you can represent given N amount of bits, you would use:
2^N - 1
But why the -1. To try understand it i created a 3 Bit systems and tried some examples:
2^1 = (2) - 1
0 0 1 --> 1
2^2 = (4) - 1
0 1 0 --> 2
0 1 1 --> 3
2^3 = (8) - 1
1 0 0 --> 4
1 0 1 --> 5
1 1 0 --> 6
1 1 1 --> 7
So it all works out as planned, but why the -1. This probably sounds like a stupid question but as you can see above i have done a fair amount of research.
Because you can represent 0 which always takes up one spot in all the permutations.
The research shown should reveal the answer already, but you have forgotten about the zero.
Three bits are able to represent 2^3 different values. The smallest value is zero, so the largest must be 2^3-1.
Note that if you use a different system (such as signed binary), the smallest and largest value may change, but the count of values does not.