Skip to content

Instantly share code, notes, and snippets.

@milanij
Last active March 21, 2018 22:46
Show Gist options
  • Save milanij/d24b6860313f0e18850810a2e88dc664 to your computer and use it in GitHub Desktop.
Save milanij/d24b6860313f0e18850810a2e88dc664 to your computer and use it in GitHub Desktop.
Calculate Primes in Go
package main
import (
"fmt"
)
func main() {
fmt.Println(Primes(200))
}
func Primes(size int) []int {
arr := make([]int, size+1)
for i := 2; i < size+1; i++ {
arr[i] = i
}
for i := 0; i < len(arr)/2+1; i++ {
if arr[i] != 0 {
twoTimesMult := 2 * i
for twoTimesMult <= size {
arr[twoTimesMult] = 0
twoTimesMult += i
}
}
}
var result []int
for _, n := range arr {
if n != 0 {
result = append(result, n)
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment