Use bitmap instead of array

This commit is contained in:
Peter J. Holzer 2021-07-12 21:09:22 +02:00 committed by Peter J. Holzer
parent 7384eaccf6
commit af97ce448c
1 changed files with 3 additions and 3 deletions

View File

@ -13,16 +13,16 @@ func main() {
panic(err) panic(err)
} }
t0 := time.Now() t0 := time.Now()
sieve := make([]bool, n) sieve := make([]uint64, (n>>7)+1)
var found int64 var found int64
var i int64 var i int64
i = 2 i = 2
found++ found++
for i = 3; i < n; i += 2 { for i = 3; i < n; i += 2 {
if !sieve[i] { if (sieve[i>>7] & (1 << ((i & 0x7F) >> 1))) == 0 {
found++ found++
for j := i * i; j < n; j += 2 * i { for j := i * i; j < n; j += 2 * i {
sieve[j] = true sieve[j>>7] |= 1 << ((j & 0x7F) >> 1)
} }
} }
} }