This example streams the prime number sequence without using trial division.
We represent the integers as a clock and assign each prime number a hand on the clock.
The 2
hand rotates the clock every other cycle, the 3
every third cycle, the 5
hits midnight
every fifth cycle, and so forth.
Here are the states of the clock for the first 16 integers:
Hands |
Positions |
Hands | 2
Positions | 0
Hands | 2 3
Positions | 1 0
Hands | 2 3
Positions | 0 1
Hands | 2 3 5
Positions | 1 2 0
Hands | 2 3 5
Positions | 0 0 1
Hands | 2 3 5 7
Positions | 1 1 2 0
Hands | 2 3 5 7
Positions | 0 2 3 1
Hands | 2 3 5 7
Positions | 1 0 4 2
Hands | 2 3 5 7
Positions | 0 1 0 3
Hands | 2 3 5 7 11
Positions | 1 2 1 4 0
Hands | 2 3 5 7 11
Positions | 0 0 2 5 1
Hands | 2 3 5 7 11 13
Positions | 1 1 3 6 2 0
Hands | 2 3 5 7 11 13
Positions | 0 2 4 0 3 1
Hands | 2 3 5 7 11 13
Positions | 1 0 0 1 4 2
Hands | 2 3 5 7 11 13
Positions | 0 1 1 2 5 3
Watch a single hand move and observe the pattern. Take the 3
hand for instance:
0
1
2
0
1
2
0
1
2