Last active
December 14, 2016 00:07
-
-
Save lukelafountaine/9e8847528b2e94f4ffea58affc6ee8a4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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