Binary palindrome checker
A Turing machine using the standard mark-and-compare technique: repeatedly mark the leftmost unmarked symbol, scan right to find the rightmost unmarked symbol, compare the two (rejecting the instant they differ), mark the second one too if they match, then scan back left past everything already marked and repeat — until no unmarked symbols remain, which means every pair matched.
When it is the right choice
As the second machine on this tool, immediately after the binary incrementer, because it is noticeably bigger for a noticeably harder kind of task: deciding whether a whole string has a property, rather than computing one new tape from an old one.
How it works
- Look at the leftmost unmarked symbol. If none remains, every pair matched: accept.
- Mark that symbol (0 becomes X, 1 becomes Y) and remember which it was.
- Scan right past the remaining unmarked symbols to find the rightmost one.
- Step back onto that rightmost unmarked symbol.
- Compare it with the remembered symbol. If they differ, reject immediately.
- If they match, mark it too.
- Scan back left, past everything already marked, to the leftmost unmarked symbol, and repeat.
Worth noticing
- The head genuinely oscillates — right, left, right, left — once per pass, and the number of passes grows with the input length. That is the mechanical reason this machine’s cost is roughly the square of the input length, where the incrementer’s is linear in it.
- The reject scenario stops the instant it finds a mismatch, part way through its very first pass — it does not run every pass to completion just because it can.
- Eight states and 28 rules against the incrementer’s three and six is the lesson: a slightly harder task costs noticeably more machine, and the cost is visible and countable here rather than asserted.
Cost
- Best
- O(n)
- Average
- O(n²)
- Worst
- O(n²)
- Space
- O(n)