First look at the source code, expected from 1 to n total all can be divisible by a or b of the sum of positive integers, in order to take advantage of the parallel advantage of the go language, using the Goroute feature to achieve, while using ordinary sequential calculation for efficiency comparison analysis
Package Chango
Import (
"FMT"
"Time"
)
Func get_sum_of_divisible (num int64, divider int64, Resultchan Chan Int64) {
var sum int64 = 0
var value int64
for value = 1; Value < num; value++ {
If Value%divider = = 0 {
Sum + = value
}
}
Resultchan <-sum
}
Func zhengchu_testing (limit int64, one int64, and one int64) {
Resultchan: = Make (chan Int64, 3)
var three int64
three = one *
T_start: = time. Now ()
Go get_sum_of_divisible (limit, one, Resultchan)
Go get_sum_of_divisible (limit, both, Resultchan)
Go get_sum_of_divisible (limit, three, Resultchan)
one_sum, two_sum, three_sum: = <-resultchan, <-resultchan, <-resultchan
var sum int64
var value int64
sum = one_sum + two_sum-three_sum
Fmt. PRINTLN (SUM)
T_end: = time. Now ()
FMT. Println (One_sum, Two_sum, three_sum)
Fmt. Printf ("Testing times1:%v\n", T_end. Sub (T_start))
sum = 0
T_start = time. Now ()
for value = 1; Value < limit; value++ {
If Value%one = = 0 {
Sum + = value
}
If Value%two = = 0 {
Sum + = value
}
If Value%three = = 0 {
Sum-= value
}
}
Fmt. PRINTLN (SUM)
T_end = time. Now ()
Fmt. Printf ("Testing times2:%v\n", T_end. Sub (T_start))
}
The Chango package is then called in the main package
Package Main
Import "./chango"
Func Main () {
Chango. Zhengchu_testing (10,3,5)
}
At first glance, there is no grammatical problem with the Chango bag, but the actual test finds that it is clear that all positive integers within 10 that can be divisible by 3 or 5, respectively, are 3,5,6,9, their sum should be 23, the sum of integers that can be 3 integers is 3+6+9=15, and the sum of positive integers divisible by 5 is 5. The sum of the positive integers that can be 3*15=15 is 0
The sum of all positive integers that are divisible by 3 or 5 within 10 by Goroute is not 23, but 13 or 13, which is why?
By debugging discovery, the problem appears in the ": =" statement
One_sum, two_sum, three_sum: = <-resultchan, <-resultchan, <-resultchan
Run multiple times
By printing the sum of positive integers that can be 3 or 5 or 3*5=15, their result is 0,18,5 or 0,5,18, and the result is not unique.
Goroute used in the go language: = Pits encountered