forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheratosthenesSieve.go
38 lines (34 loc) · 899 Bytes
/
eratosthenesSieve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"fmt"
)
//Only works for primes smaller or equal to 10e7
func sieve(upperBound int64) []int64 {
_sieveSize := upperBound + 10
//Creates set to mark wich numbers are primes and wich are not
//true: not primes, false: primes
//this to favor default initialization of arrays in go
var bs [10000010]bool
//creates a slice to save the primes it finds
primes := make([]int64, 0, 1000)
bs[0] = true
bs[1] = true
//iterate over the numbers set
for i := int64(0); i <= _sieveSize; i++ {
//if find one number that is not marked as a compund number, mark all its multiples
if !bs[i] {
for j := i * i; j <= _sieveSize; j += i {
bs[j] = true
}
//Add the prime you just find to the slice of primes
primes = append(primes, i)
}
}
return primes
}
func main() {
//prints first N primes into console
N := 100
primes := sieve(N)
fmt.Println(primes)
}