From af97ce448c44468725ddef9f4ea2356e8dfb9146 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Mon, 12 Jul 2021 21:09:22 +0200 Subject: [PATCH] Use bitmap instead of array --- eratosthenes_parallel.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eratosthenes_parallel.go b/eratosthenes_parallel.go index c57c174..b327807 100644 --- a/eratosthenes_parallel.go +++ b/eratosthenes_parallel.go @@ -13,16 +13,16 @@ func main() { panic(err) } t0 := time.Now() - sieve := make([]bool, n) + sieve := make([]uint64, (n>>7)+1) var found int64 var i int64 i = 2 found++ for i = 3; i < n; i += 2 { - if !sieve[i] { + if (sieve[i>>7] & (1 << ((i & 0x7F) >> 1))) == 0 { found++ for j := i * i; j < n; j += 2 * i { - sieve[j] = true + sieve[j>>7] |= 1 << ((j & 0x7F) >> 1) } } }