liveBook · Manning

Prime number pipeline algorithm

As explained in Learn Concurrent Programming with Go

The prime number pipeline algorithm generates prime numbers by passing candidate integers through stages that filter multiples of previously found primes.

The prime number pipeline algorithm generates primes by sending candidate integers through a chain of goroutines. Each stage represents a discovered prime and filters out its multiples; survivors continue to the next stage. section 9.2.7

A dynamic Go pipeline proceeds as follows:

  • Create a new goroutine and channel when a surviving value reaches the chain's end. section 9.2.7

Generating primes below 100,000

The main function feeds integers from 2 through 99,999 into the pipeline. Its output begins 2, 3, 5, 7, 11, 13 and ends with 99989 and 99991.

Figure 9.13 Checking to see whether a number is a prime by using a pipeline

Visualizes candidate numbers passing through prime-filtering stages. section 9.2.7

Figure 9.14 When a new prime is found, we start a new goroutine that filters multiples of that new prime.

Shows how discovering a prime extends the pipeline with another filtering goroutine. section 9.2.7

How Learn Concurrent Programming with Go covers it

How deep it goes
Explains dynamic stage creation, filtering, channel closure, and termination, while simplifying primality checks.
Examples
Uses Go listings that filter candidates and produce primes from 2 through 99,999.
What you can do after
Build it from scratch with Go goroutines, channels, filtering stages, and completion signaling.
How it connects
Connects goroutines and channels with concurrency, message passing, and CSP-style pipelines.

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.