diff --git a/eratosthenes_parallel.go b/eratosthenes_parallel.go index 517e34a..0489826 100644 --- a/eratosthenes_parallel.go +++ b/eratosthenes_parallel.go @@ -36,8 +36,16 @@ func main() { p := nCpus var jnext int64 + // XXX - i * i can overflow. + // bail out before that happens + if n / i < i { + continue; + } for j := i * i; j < n; j = jnext { jnext = ((n-j)/(2*i)/p+1)*(2*i) + j + if jnext > n { + jnext = n + } markGroup.Add(1) go markMultiples(sieve, i, j, jnext) p--