-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimes.go
57 lines (46 loc) · 853 Bytes
/
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
package main
import (
"flag"
"fmt"
"os"
"strconv"
)
var goal int
func primetask(c chan int) {
p := <-c
if p > goal {
os.Exit(0)
}
fmt.Println("p is ", p)
nc := make(chan int)
go primetask(nc)
for {
i := <-c
//fmt.Println("i is ", i)
if i%p != 0 {
fmt.Println("nc is ", i)
nc <- i
}
}
}
func main() {
flag.Parse()
args := flag.Args()
if args != nil && len(args) > 0 {
var err error
goal, err = strconv.Atoi(args[0])
if err != nil {
goal = 100
}
} else {
goal = 100
}
fmt.Println("goal = ", goal)
c := make(chan int)
go primetask(c)
fmt.Println("start task")
for i := 2 ;; i++ {
fmt.Println("in the loop", i)
c <- i
}
}