block by bmershon e15a65d5599870a860de734f2ef09cde

Number of Primes

Full Screen

A proposed solution to exercise 3.3 from Stepanov and Rose’s From Mathematics to Generic Programming. The exercise asks us to graph the number of primes less than n using an analytic approximation.

A Web Worker performs each test of increasing input size and reports back to the main thread with the number of primes found that are less than the input. Only one test actually needs to be run in order to graph the prime-counting function; however, by running each test separately in a WebWorker (each with a successively larger different input size), one can perform a complexity analysis of the various implementations of the Sieve of Eratosthenes.

Using Web Workers in this example also helps us avoid locking up the main user interface thread while the algorithm is executing.

index.html

worker.js