a-conjecture-of-mine

An exercise on polyglossy: the same problem solved on multiple languages

commit 050490bc01d8f86060f91d6c94c159278473251f
parent f7d801fc7978968a707ebe619ebb27e73908bb7e
Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date:   Sat,  1 Jun 2019 16:29:33 -0300

Implemented an iterator approach for distributing calculations across threads.

Diffstat:
MGo/conjecture.go | 43+++++++++++++++++++------------------------
MREADME.md | 2+-
2 files changed, 20 insertions(+), 25 deletions(-)
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)
+}
diff --git a/README.md b/README.md
@@ -13,7 +13,7 @@ The conjecture was [proved by exhaustion](https://en.wikipedia.org/wiki/Proof_by
 |--------------|------------|
 |**Rust**      |115         |
 |**Kotlin**    |137         |
-|**Go**        |138         |
+|**Go**        |155         |
 |**C**         |271         | 
 |**JavaScript**|656         |
 |**Ruby**      |9850        |