-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimenumbers.go
More file actions
74 lines (70 loc) · 1.4 KB
/
primenumbers.go
File metadata and controls
74 lines (70 loc) · 1.4 KB
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
package algorithm
//PrimeNumbersBuilder 质数筛
func PrimeNumbersBuilder(max int) []int {
ans := make([]int, 0)
isPrime := make([]bool, max+1)
for i := 2; i <= max; i++ {
isPrime[i] = true
}
for i := 2; i*i <= max; i++ {
if isPrime[i] {
for j := i * i; j <= max; j += i {
isPrime[j] = false
}
}
}
for i := 2; i <= max; i++ {
if isPrime[i] {
ans = append(ans, i)
}
}
return ans
}
// PrimeNumbersChanBuilder 管道筛选质数
// 纯属好玩,无法提升运行时间,因为管道是串联的
// 甚至因为有协程的切换而降低效率
func PrimeNumbersChanBuilder(max int) []int {
ans := make([]int, 0)
ch, exit := genarateChan(max)
for {
select {
case prime := <-ch:
ans = append(ans, prime)
ch, exit = primeFilter(ch, exit, prime)
case <-exit:
return ans
}
}
}
func genarateChan(max int) (<-chan int, <-chan struct{}) {
ch := make(chan int)
exit := make(chan struct{})
go func() {
for i := 2; i <= max; i++ {
ch <- i
}
close(exit)
}()
return ch, exit
}
func primeFilter(ch <-chan int, exit <-chan struct{}, prime int) (<-chan int, <-chan struct{}) {
filterCh := make(chan int)
filterExit := make(chan struct{})
go func() {
for {
select {
case i, ok := <-ch:
if ok {
if i%prime != 0 {
filterCh <- i
}
}
case <-exit:
close(filterCh)
close(filterExit)
return
}
}
}()
return filterCh, filterExit
}