diff --git a/Go/conjecture.go b/Go/conjecture.go
@@ -9,7 +9,6 @@ import (
"bufio"
"fmt"
"math"
- "math/rand"
"os"
"strconv"
"strings"
@@ -41,7 +40,7 @@ func main() {
fmt.Println("\nLOADING. . .")
tuple, found, elepsed := getCounterexample(max, nThreads)
- fmt.Printf("LOADED in %dms\n [%d Go Routines]", elepsed.Nanoseconds()/int64(math.Pow10(6)), nThreads)
+ fmt.Printf("LOADED in %dms [%d Go Routines]\n", elepsed.Nanoseconds()/int64(math.Pow10(6)), nThreads)
if !found {
fmt.Printf("\nThe conjecture is proved for all natural numbers smaller or equals to %d!\n", max)
} else {
@@ -60,28 +59,13 @@ func getCounterexample(max uint, nThreads uint) (value [2]uint, found bool, elep
if nThreads > 1 {
channels := make([](chan [2]uint), nThreads)
- // Shuffle the numbers from 0 to max
- for i, n := range append(rand.Perm(int(max)), 0) {
- mRange[i] = uint(n)
- }
-
- ranges := make([][]uint, nThreads)
- rangeLen := int((max + 1) / nThreads)
-
- // Subdivide mRange into nThread sub-ranges
+ // Compute the result of each sub-range
for i := 0; i < int(nThreads); i++ {
- ranges[i] = mRange[i : (i+1)*rangeLen]
+ iter := make(chan uint)
channels[i] = make(chan [2]uint)
- }
- for i := rangeLen*int(nThreads) + 1; i <= int(max); i++ {
- r := &ranges[i%int(nThreads)]
- *r = append(*r, mRange[i])
- }
-
- // Compute the result of each sub-range
- for i, tRange := range ranges {
- go getCounterRange(tRange, max, &sums, channels[i])
+ go arithmeticProg(iter, uint(i), nThreads, max)
+ go getCounterRange(iter, max, &sums, channels[i])
}
// Listen for the computation to finish
@@ -97,7 +81,10 @@ func getCounterexample(max uint, nThreads uint) (value [2]uint, found bool, elep
mRange[i] = uint(i)
}
- go getCounterRange(mRange, max, &sums, c)
+ iter := make(chan uint)
+ go arithmeticProg(iter, 0, nThreads, max)
+ go getCounterRange(iter, max, &sums, c)
+
for msg := range c {
return msg, true, time.Since(start)
}
@@ -106,8 +93,8 @@ func getCounterexample(max uint, nThreads uint) (value [2]uint, found bool, elep
return [2]uint{0, 0}, false, time.Since(start)
}
-func getCounterRange(rang []uint, max uint, sums *[]int, c chan [2]uint) {
- for _, a := range rang {
+func getCounterRange(rang chan uint, max uint, sums *[]int, c chan [2]uint) {
+ for a := range rang {
for b := a; b <= max; b++ {
diff := (*sums)[a+b] - (*sums)[a] - (*sums)[b]
@@ -146,3 +133,11 @@ func sumDigits(n uint) int {
n /= 10
}
}
+
+func arithmeticProg(iter chan uint, start uint, delta uint, bound uint) {
+ for i := start; i <= bound; i += delta {
+ iter <- i
+ }
+
+ close(iter)
+}