Avoid overflow

This commit is contained in:
Peter J. Holzer 2021-07-13 11:17:37 +02:00 committed by Peter J. Holzer
parent 12cf4f79ff
commit cfb18dec75
1 changed files with 8 additions and 0 deletions

View File

@ -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--