-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes.go
95 lines (86 loc) · 1.72 KB
/
primes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"flag"
"fmt"
)
func generate(ch chan<- uint, start, end uint) {
defer close(ch)
if end == 0 {
for i := uint(3); i < start<<1; i += 2 {
ch <- i
}
} else {
for i := uint(3); i < end; i += 2 {
ch <- i
}
}
}
func sieve(src <-chan uint, dst chan<- uint, prime uint) {
defer close(dst)
for i := range src {
if i%prime != 0 {
dst <- i
}
}
}
func sieveChannel(src <-chan uint, start, prime uint) <-chan uint {
src_prime := make(chan uint, 1)
go sieve(src, src_prime, prime)
return src_prime
}
func nextPrime(src <-chan uint, prime uint) uint {
for i := range src {
return i
}
return prime
}
func nextPrimeSend(src <-chan uint, dst chan<- uint, prime uint) uint {
for i := range src {
dst <- i
return i
}
return prime
}
func filterRoutine5(src <-chan uint, dst chan<- uint, start, end, prime uint) {
defer close(dst)
for last_prime := uint(0); prime < start && last_prime != prime; {
last_prime = prime
src = sieveChannel(src, start, prime)
prime = nextPrime(src, prime)
}
if start <= 2 {
dst <- 2
if end > 0 {
dst <- prime
}
}
if end == 0 {
for range src {
}
}
for last_prime := uint(0); last_prime != prime; {
last_prime = prime
src = sieveChannel(src, start, prime)
prime = nextPrimeSend(src, dst, prime)
}
}
func startSieve(start, end uint) <-chan uint {
src := make(chan uint, 1)
go generate(src, start, end)
dst := make(chan uint, 1)
go filterRoutine5(src, dst, start, end, <-src)
return dst
}
func main() {
var start uint
var end uint
flag.UintVar(&start, "start", 0, "usage")
flag.UintVar(&end, "end", 0, "usage")
flag.Parse()
if end > 0 && end <= 2 {
return
}
for prime := range startSieve(start, end) {
fmt.Print(prime, "\n")
}
}