Skip to content

Instantly share code, notes, and snippets.

@tylerkerr
Last active June 18, 2017 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerkerr/3d66e7f57bb55321c64bfb4c75c188c3 to your computer and use it in GitHub Desktop.
Save tylerkerr/3d66e7f57bb55321c64bfb4c75c188c3 to your computer and use it in GitHub Desktop.
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