-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayWriterPrimeFinder.go
More file actions
53 lines (46 loc) · 1.07 KB
/
Copy patharrayWriterPrimeFinder.go
File metadata and controls
53 lines (46 loc) · 1.07 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
/*
* Attempt to find all primes up to 500k by splitting the workload between different cores
* Instead of only testing the
*/
package main
import (
"fmt"
"time"
"sync"
)
const max_test = 500000
// Set all indexes is map which is
func removeCandidates(n int, prime_map *[max_test+1]bool, wg *sync.WaitGroup){
for i := n*2; i < max_test+1; i+=n {
prime_map[i] = true
}
wg.Done()
}
func countTrue(in [max_test+1]bool) (int) {
count := 0
for _, n := range in {
if (!n) {
//fmt.Printf("%d is prime!\n", i)
count++
}
}
return count
}
func setConventions(in *[max_test+1]bool) {
in[0] = true
in[1] = true
}
func main() {
start := time.Now()
var dividables [max_test+1]bool
setConventions(&dividables)
var wg sync.WaitGroup
for i := 2; i <= max_test; i+=1 {
wg.Add(1)
go removeCandidates(i, &dividables, &wg)
}
wg.Wait()
count := countTrue(dividables)
elapsed := time.Since(start)
fmt.Printf("Found %d primes in %s \n", count + 1, elapsed)
}