-
-
Save tylerkerr/3d66e7f57bb55321c64bfb4c75c188c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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