Skip to content

Instantly share code, notes, and snippets.

@youknowcast
Created July 7, 2019 07:32
Show Gist options
  • Save youknowcast/4263b376726a59895bea83c587008f6f to your computer and use it in GitHub Desktop.
Save youknowcast/4263b376726a59895bea83c587008f6f to your computer and use it in GitHub Desktop.
eratosthenes with golang
package main
import (
"fmt"
"math"
)
const NUM = 100000
func main() {
prime := make([]int, NUM+1)
var i,j,limit int
for i = 2; i < NUM-1; i++ {
prime[i] = 1
}
limit = int(math.Sqrt(float64(NUM)))
for i=2; i <= limit; i++ {
if prime[i] == 1 {
for j=2*i; j <=NUM; j++ {
if j % i == 0 {
prime[j] = 0
}
}
}
}
for i=2; i <=NUM; i++ {
if prime[i] == 1 {
fmt.Print("\n")
fmt.Print(i)
}
}
fmt.Print("\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment