eratosthenes_parallel/eratosthenes_parallel.go

34 lines
518 B
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"os"
"strconv"
"time"
)
func main() {
n, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil {
panic(err)
}
t0 := time.Now()
2021-07-12 21:09:22 +02:00
sieve := make([]uint64, (n>>7)+1)
var found int64
var i int64
i = 2
found++
for i = 3; i < n; i += 2 {
2021-07-12 21:09:22 +02:00
if (sieve[i>>7] & (1 << ((i & 0x7F) >> 1))) == 0 {
found++
for j := i * i; j < n; j += 2 * i {
2021-07-12 21:09:22 +02:00
sieve[j>>7] |= 1 << ((j & 0x7F) >> 1)
}
}
}
d := time.Since(t0)
fmt.Println()
fmt.Println(found, "primes found in", d)
}