File tree 3 files changed +63
-0
lines changed
algorithm/coro-prime-sieve
3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "bufio"
5
+ "fmt"
6
+ "io"
7
+ "os"
8
+ "strconv"
9
+ )
10
+
11
+ // get r3kt.
12
+ // - yung innanet
13
+
14
+ func SieveOfEratosthenes (out io.Writer ) {
15
+ sieve := make ([]bool , 20000 )
16
+ p := 2
17
+
18
+ for {
19
+ if ! sieve [p ] {
20
+ _ , _ = fmt .Fprintln (out , p )
21
+ for multiple := p * p ; multiple <= len (sieve )- 1 ; multiple += p {
22
+ sieve [multiple ] = true
23
+ }
24
+ }
25
+ p ++
26
+ if p > len (sieve )- 1 {
27
+ return
28
+ }
29
+ }
30
+ }
31
+
32
+ func main () {
33
+ n := 1000
34
+ if len (os .Args ) > 1 {
35
+ if _n , err := strconv .Atoi (os .Args [1 ]); err == nil {
36
+ n = _n
37
+ }
38
+ }
39
+ pipeIn , pipeOut := io .Pipe ()
40
+
41
+ out := bufio .NewWriter (os .Stdout )
42
+ in := bufio .NewWriter (pipeOut )
43
+
44
+ readerUntil := bufio .NewReader (pipeIn )
45
+ go SieveOfEratosthenes (in )
46
+
47
+ rd := func () []byte {
48
+ b , _ := readerUntil .ReadSlice ('\n' )
49
+ return b
50
+ }
51
+
52
+ i := 0
53
+ for {
54
+ if i >= n {
55
+ break
56
+ }
57
+ _ , _ = out .Write (rd ())
58
+ i ++
59
+ }
60
+ _ = out .Flush ()
61
+ }
Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ problems:
45
45
- name : coro-prime-sieve
46
46
source :
47
47
- 1.go
48
+ - 2.go
48
49
- name : http-server
49
50
source :
50
51
- 1.go
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ problems:
40
40
- name : coro-prime-sieve
41
41
source :
42
42
- 1.go
43
+ - 2.go
43
44
# - name: json-serde
44
45
# source:
45
46
# - 1.go
You can’t perform that action at this time.
0 commit comments