Skip to content

Commit

Permalink
Solve #7 in Go.
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharydenton committed Jun 12, 2015
1 parent 83a0bb1 commit a6b3d88
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 007/10001st-prime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import "fmt"

func iSqrt(n int64) int64 {
var r1, r int64 = n, n + 1
for r1 < r {
r, r1 = r1, (r1+n/r1)>>1
}
return r
}

func PrimeSieve(n int64) []int64 {
result := make([]int64, 0, n)
sieve := make([]bool, n+1)
sn := iSqrt(n)
var i, j int64
for i = 2; i <= sn; i++ {
if !sieve[i] {
for j = i * i; j <= n; j += i {
sieve[j] = true
}
}
}
for i = 2; i <= n; i++ {
if !sieve[i] {
result = append(result, i)
}
}
return result
}

func main() {
primes := PrimeSieve(1000000)
fmt.Println(primes[10000])
}

0 comments on commit a6b3d88

Please sign in to comment.