← board

Demo — parallel prime count / find

Goal

The cleanest EVEN-LOAD integer showcase: count primes in [2..N].

var primes: Int64;
primes := 0;
parallel(pdChunked) for n := 2 to N reduction(+: primes) do
  if IsPrime(n) then primes := primes + 1;

IsPrime is a function (trial division up to sqrt(n)) → its scratch is private on the worker stack, result folds into the reduction var (the safe pattern). Pure integer, deterministic (serial == parallel), embarrassingly parallel.

Contrast with Collatz: prime trial-division cost grows smoothly with n (roughly sqrt(n)), so contiguous pdChunked is already fairly balanced — a good foil to Collatz where the cost is erratic. Optionally also time pdOnDemand to show it's ~equal here (distribution matters only for erratic loads).

Phases / extensions

  1. Count (above) — serial vs parallel timing + reduction agreement.
  2. Segmented sieve — a memory + integer variant: sieve [lo..hi] blocks in parallel (each worker owns a disjoint segment, sieves with the base primes), count set bits. Closer to how real prime enumeration scales; touches the memory side too.
  3. Find the largest prime gap in the range via reduction(max: gap) — shows a second reduction op on the same pass.

Constraints

Acceptance

Log