forked from mo2g/DevLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum.go
75 lines (65 loc) · 1.07 KB
/
sum.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
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"runtime"
"time"
)
/*
n 内 能被 2、3、5、6 整除的数 相加
*/
func main() {
runtime.GOMAXPROCS(2)
x := 0
time1 := time.Now()
x = _sum(1, 10000000)
time2 := time.Now()
x = go_sum(3, 10000000)
time3 := time.Now()
fmt.Println(x)
fmt.Println(time2.Sub(time1))
fmt.Println(time3.Sub(time2))
}
func _sum(start int, end int) int {
sum := 0
for i := start; i < end; i++ {
if i%2 == 0 || i%3 == 0 || i%5 == 0 || i%6 == 0 {
sum += i
}
}
return sum
}
func chan_sum(start int, end int, totle chan int) {
sum := 0
for i := start; i < end; i++ {
if i%2 == 0 || i%3 == 0 || i%5 == 0 || i%6 == 0 {
sum += i
}
}
totle <- sum
}
/*123 456 789 10*/
func go_sum(num int, max int) int {
sum := 0
y := max / num
start := 0
end := 0
if max%num != 0 {
num += 1
}
totle := make(chan int, num)
for i := 0; i < num; i++ {
start = y * i
end = y * (i + 1)
if start == 0 {
start = 1
}
if end >= max {
end = max
}
go chan_sum(start, end, totle)
}
for i := 0; i < num; i++ {
sum += <-totle
}
return sum
}