Skip to content

Instantly share code, notes, and snippets.

@Heliosmaster
Created September 8, 2012 17:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Heliosmaster/3677584 to your computer and use it in GitHub Desktop.
Save Heliosmaster/3677584 to your computer and use it in GitHub Desktop.
Simple Erathostenes' Sieve in Go
package main
import (
"fmt"
"os"
"math"
"strconv"
)
func main(){
// figures out which number is the bound
n,err := strconv.ParseFloat(os.Args[1],64)
if err != nil {
fmt.Printf("Error converting parameter to int\n")
return
}
sqrt := int(math.Sqrt(n))
// creates the starting array
a:=make([]int,int(n)/2)
a[0]=2;
for i:=1;i<len(a);i++ {
a[i]=2*i+1
}
//sieves
for i:=1;i<sqrt; i++{
m := a[i]
if m == 0 {
continue
}
for j:=i*(m+1);j<len(a);j+=m {
a[j]=0
}
}
//counts the number of primes
count := 0
for i:=0;i<len(a);i++{
if a[i]!=0 {
count++
}
}
fmt.Printf("number of primes %d\n",count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment