📘 CodingMarble Learn

Turing Machine and Theory of Algorithms

A Turing machine is a very simple imaginary computer: a long tape, a head that reads and writes one cell, and a small rule table. The Church–Turing thesis says that anything we can compute by a clear step-by-step method, such a machine can compute too. Complexity analysis counts how the number of steps grows as the input grows.

🎬 Step-by-step story

  1. Here is a tape with cells. It holds 01011, the number 11. The red head sits on the first digit.
  2. The head can do only three things: read one cell, write one cell, move one cell. We just moved it one step right.
  3. Rule 1: keep going right until you reach an empty cell. Watch the head run to the end of the number.
  4. Rule 2: go back left. Turn each 1 into 0. The first 0 becomes 1. Stop. 01011 became 01100, which is 12. We added 1!
  5. Count the steps. A number with n digits needs about 2n + 1 steps. The red bars show a method that needs 2ⁿ steps. It explodes.
  6. Free play. Pick any number and press Run. Count the steps and check the answer yourself.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why is the tape endless?

So the machine never runs out of memory. A real computer has limited memory, but we imagine an endless tape to study what is possible at all.

Can the head jump to a far cell directly?

No. It moves only one cell at a time. That is why steps are counted, and why going right to the end costs one step per digit.

Why does the machine go right first?

It must find the END of the number, because addition starts from the last digit.

Why does 1 become 0 while going left?

Adding 1 to a 1 gives 10: write 0 and carry 1 to the next digit on the left, just like decimal addition.

Why is a 2ⁿ-step method called hopeless?

Each extra input doubles the work. The red bars go off the top quickly, so even a small n takes years.

Does more digits always mean more steps?

For this machine, yes: about 2 steps per digit. Move the slider to larger numbers and compare the step counter.

What is a Turing machine?

In 1936 Alan Turing imagined the simplest computer he could. It has four parts.

The machine starts, follows the rules one by one, and stops when it reaches a halt state. Whatever is on the tape then is the answer.

Example: add 1 to a binary number

The tape holds 01011. We want 01100.

StateReadsWritesMovesNext state
go0 or 1samerightgo
goblankblankleftcarry
carry10leftcarry
carry0 or blank1stayhalt

Rule "carry, read 1, write 0" is the same as how you add by hand: 1 + 1 = 10, write 0 and carry 1 to the left.

Church–Turing thesis

People invented other models of computing too: Alonzo Church made one using functions (lambda calculus), and later came real programming languages. Every time, they turned out to be equal in power to a Turing machine.

The Church–Turing thesis says: if a problem can be solved by a clear step-by-step method (an algorithm), then a Turing machine can solve it.

It is called a thesis, not a theorem, because "clear step-by-step method" is an idea, not a maths definition, so it cannot be proved. But nobody has found a counter-example in about 90 years.

A consequence: some things are not computable by any machine. The famous one is the halting problem: no program can always tell, for every other program, whether it will finish or run forever.

Complexity analysis: counting steps

An algorithm that works is not enough. It must also be fast enough. Time complexity asks: if the input size is n, how many steps are needed? We ignore small details and keep the main growth, using Big O.

StepsNameIf n doubles
nO(n) linearabout 2 times
n²O(n²) quadraticabout 4 times
2ⁿO(2ⁿ) exponentialsquared! hopeless

Our add-1 machine takes about 2n + 1 steps, so it is O(n). Space complexity counts the memory used, for example the number of tape cells.

Class P is the set of problems with a polynomial-time algorithm (n, n², n³ …). Whether every problem whose answer is easy to check is also easy to solve (P vs NP) is still an open question.

Try it: be the Turing machine

Take a strip of paper and draw 7 boxes. Write 0 1 1 0 1 in the middle five. Put a coin on the first digit. Use ONLY the rule table above. For each move, say the state aloud and count it. Then use the 3D: pick the number 13 and press Run. Predict the steps first, then check.

Key formulas and definitions

Worked examples

1. The tape is 0 1 0 0 1 (9). Run the add-1 machine. What is the final tape and how many steps?

The head goes right over 5 digits, one step each (5 steps), moves to the blank (1 step), then goes left: the last digit is 1, so it becomes 0 and the head moves left (1 step); the next digit is 0, so it becomes 1 and the machine halts (1 step). Final tape 0 1 0 1 0 = 10. In total 8 steps.

2. An algorithm takes 3n + 5 steps. What is its Big O?

Drop the constant 5 and the factor 3. It is O(n).

3. An algorithm takes n² steps. For n = 10 it takes 100 steps. How many for n = 30?

30² = 900 steps. n became 3 times bigger, the steps became 3² = 9 times bigger.

4. A method needs 2ⁿ steps. A computer does 10⁹ steps per second. Is n = 60 possible?

2⁶⁰ is about 1.15 × 10¹⁸ steps. Time = 1.15 × 10¹⁸ / 10⁹ = 1.15 × 10⁹ seconds, about 36 years. So n = 60 is not practical.

5. A Turing machine is in state "go" reading a blank. By the table, what does it do?

It writes a blank, moves left and goes to state "carry".

6. Can a normal laptop solve a problem that no Turing machine can solve?

No. By the Church–Turing thesis, every algorithm a laptop runs can also be run on a Turing machine. A laptop is only faster.

Common mistakes

Practice quiz

1. Which part of a Turing machine reads and writes symbols?
2. The Church–Turing thesis says an algorithm can be done by:
3. An algorithm needs 5n² + 2n steps. Its Big O is:
4. Which grows fastest as n becomes large?
5. The halting problem shows that:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

Why did Turing invent the Turing machine?

He wanted a clear, exact meaning for "what can be computed". A very simple machine made it possible to prove that some problems have no algorithm.

Is Big O the exact number of steps?

No. It shows only the growth order for large n and ignores constants and small terms.

What is P vs NP?

It asks: if a solution can be checked quickly, can it also be found quickly? Nobody knows the answer yet.

Learn first

Learn next

Related lessons

All Computer Science lessons