-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.go
66 lines (54 loc) · 1.1 KB
/
10.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
package main
import (
"fmt"
"math"
)
// Find all the primes < n, naively.
// This takes about 100s for n=2M.
func primes(n int, c chan int) {
primes := make([]int, 1)
primes[0] = 2
c <- 2
foreach_i:
for i := 3; i < n; i += 2 {
for _, p := range primes {
if i%p == 0 { // not a prime
continue foreach_i
}
}
primes = append(primes, i)
c <- i
}
close(c)
}
// Find all the primes < n, using Erastotsthenes' sieve.
// This takes about 0.4s for n=2M, but is bounded by RAM.
func fast_primes(n int, c chan int) {
composite := make([]bool, n)
for i := 2; i <= int(math.Sqrt(float64(n))); i++ {
if composite[i] { // keep walking until we hit the next prime
continue
}
for j := i * i; j < n; j += i {
composite[j] = true
}
}
// Deal with 0, 1 as special cases.
composite[0], composite[1] = true, true
for i, cmp := range composite {
if !cmp {
c <- i
}
}
close(c)
}
func main() {
n, p := 2000000, 0
sum := int64(0)
c := make(chan int)
go fast_primes(n, c)
for p = range c {
sum += int64(p)
}
fmt.Printf("The sum of the prime numbers < %d is %d.\n", n, sum)
}