Skip to content

Instantly share code, notes, and snippets.

@lukelafountaine
Last active December 14, 2016 00:07
Show Gist options
  • Select an option

  • Save lukelafountaine/9e8847528b2e94f4ffea58affc6ee8a4 to your computer and use it in GitHub Desktop.

Select an option

Save lukelafountaine/9e8847528b2e94f4ffea58affc6ee8a4 to your computer and use it in GitHub Desktop.
func NextPrime() <-chan int {
out := make(chan int, 1)
go func() {
out <- 2
composites := make(map[int][]int)
num := 3
for {
if _, ok := composites[num]; !ok {
out <- num
composites[num * num] = []int{num}
} else {
for _, prime := range composites[num] {
next := num + prime
for next % 2 == 0 {
next += prime
}
if _, ok := composites[next]; ok {
composites[next] = append(composites[next], prime)
} else {
composites[next] = []int{prime}
}
}
delete(composites, num)
}
num += 2
}
}()
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment