# Flip prime computer

The following program computes the sequence of prime numbers.

```'X>'v
2X'<
vZv
p + 0
X/ /|
@
^
vXv 0
@>+X
>,>,<
+> ^+
```

Here is a breakdown of how the program works.

```'X>'v
2X'<

p
```

The first section of the program simply prints the number 2 and then creates a stream of odd numbers (starting from 3) (one every 6 ticks) heading down from the lower processor.

```  vZv
p + 0
X/ /|
@
^
```

The second section creates a loop with two sluices and the two flippers. Candidate primes from the first section enter the loop at the upper left sluice and continue anticlockwise around the loop, climing one level each cycle. They leave the loop in one of two ways. If they reach a level that no number has previously, the left-hand flipper sends them left instead of right (and flips, ensuring subsequent balls at this level will continue around the loop). In this case the number is confirmed prime and is both printed and sent on to the third section. Alternatively, if a 0-ball (from the next part of the program) on the same level hits the right-hand flipper with the correct timing, the ball is redirected down and then out of the program. (The zero ball is sent out of the program by the upper right sluice). This happens if the number is a multiple of the prime that was found on this level.

```vXv 0
@>+X
>,>,<
+> ^+
```

The third section operates independently on each level. Its purpose is to generate a 0-ball (heading upwards from the number generator) every 6p ticks, where p is the prime found on this level. The key portion is this part:

```    0
@>+X
>,<
+> ^
```

one ball (with value p) waits on the tarpit and another makes p-1 cycles anticlockwise around the lower four sluices until it is reduced to 0 and directed up and then right onto the tarpit (computing p+0=p). Then it is cloned, with one clone returning to the lower loop and the other generating a 0-ball upwards before being cloned again onto the tarpit. The remaining code around this part initialises the tarpit and puts a ball into the loop, slightly reduced to ensure the first 0 created is timed to intercept the number 3p in the second section.