Automata and Turing machines

O(n)·O(1)
q0, back to itself: 00q0 to q1: 11q1 to q2: 00q1 to q0: 11q2 to q1: 00q2, back to itself: 11q0 (remainder 0)q1 (remainder 1)q2 (remainder 2)
1111000022InputPosition
  • Being read

The machine starts in state q0, before reading any symbols.

The machine starts in state q0, before reading any symbols.
  1. 1While the remainder so far is 0, reading a 0 keeps the remainder at 0.
  2. 2While the remainder so far is 0, reading a 1 makes the remainder 1.
  3. 3While the remainder so far is 1, reading a 0 makes the remainder 2.
  4. 4While the remainder so far is 1, reading a 1 makes the remainder 0.
  5. 5While the remainder so far is 2, reading a 0 makes the remainder 1.
  6. 6While the remainder so far is 2, reading a 1 keeps the remainder at 2.
Symbols read
0

Complexity

BestO(n)
AverageO(n)
WorstO(n)
Extra spaceO(1)
n at n=3 predicts 3; this run took 0.

Divisible by three

A deterministic finite automaton with three states, one per possible remainder (0, 1 or 2) of the binary number read so far, modulo three. Reading a bit b in state r moves to state (2r + b) mod 3, because shifting a binary number one place left and adding a bit multiplies its value by two and adds that bit. The number is a multiple of three exactly when the machine ends in state q0.

When it is the right choice

As the module’s example of designing (or reading) a hand-built automaton directly as a language acceptor, which is a different skill from `/regex`’s own NFA and DFA pages: those derive their automaton from a pattern, by a construction algorithm. This one is simply asserted correct, the way a textbook presents it, and then checked.

How it works

Worth noticing

Cost

Best
O(n)
Average
O(n)
Worst
O(n)
Space
O(1)

Worth comparing

All automata and Turing machines