Skip to content

Instantly share code, notes, and snippets.

@tylerkerr tylerkerr/gosieve.go Secret
Last active Jun 18, 2017

Embed
What would you like to do?
package main
import (
"fmt"
"math"
"os"
"strconv"
)
func main() {
n, _ := strconv.Atoi(os.Args[1])
primes := make([]bool, n+1)
primes[0], primes[1] = true, true // 0 and 1 are not primes
max := int(math.Ceil(math.Sqrt(float64(n))))
for i := 2; i <= max; i++ { // the sieve
if primes[i] == false {
for j := i * i; j <= n; j += i {
primes[j] = true
}
}
}
count := 0
for _, v := range primes {
if v == false {
count += 1
}
}
fmt.Println(count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.