Last active
March 21, 2018 22:46
-
-
Save milanij/d24b6860313f0e18850810a2e88dc664 to your computer and use it in GitHub Desktop.
Calculate Primes in Go
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" | |
) | |
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