- Commit
- b4ff4be394817bc01b2ad9337da8c61446e092b1
- Parent
- b39948f6cf5be0685a4259be90109e65b6266638
- Author
- Pablo Escobar Gaviria <gark.garcia@protonmail.com>
- Date
Fixed the Go implementation and updated it's benchmarking information.
An exercise on polyglossy: the same problem solved on multiple languages
Fixed the Go implementation and updated it's benchmarking information.
2 files changed, 79 insertions, 79 deletions
Status | File Name | N° Changes | Insertions | Deletions |
Modified | Go/main.go | 156 | 78 | 78 |
Modified | README.md | 2 | 1 | 1 |
diff --git a/Go/main.go b/Go/main.go @@ -6,14 +6,14 @@ package main // For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger. import ( - "os" - "strconv" + "os" + "strconv" ) type iter struct { - Start uint - End uint - Step uint + Start uint + End uint + Step uint } const success = 0 @@ -21,102 +21,102 @@ const fail = 1 const invalidInput = 2 func main() { - if len(os.Args) > 0 { - var nGoRoutines uint = 1 - - if len(os.Args) > 1 { - arg, err := strconv.ParseUint(os.Args[2], 10, 64) - if err == nil && arg >= 1 { - nGoRoutines = uint(arg) - } - } - - max, err := strconv.ParseUint(os.Args[0], 10, 64) - if err == nil { - if counterexample(uint(max), nGoRoutines) { - os.Exit(fail) - } else { - os.Exit(success) - } - } else { - os.Exit(invalidInput) - } - } else { - os.Exit(invalidInput) - } + if len(os.Args) > 1 { + var nGoRoutines uint = 1 + + if len(os.Args) > 2 { + arg, err := strconv.ParseUint(os.Args[2], 10, 64) + if err == nil && arg >= 1 { + nGoRoutines = uint(arg) + } + } + + max, err := strconv.ParseUint(os.Args[1], 10, 64) + if err == nil { + if counterexempl(uint(max), nGoRoutines) { + os.Exit(fail) + } else { + os.Exit(success) + } + } else { + os.Exit(invalidInput) + } + } else { + os.Exit(invalidInput) + } } -func counterexample(max uint, nGoRoutines uint) bool { - sums := getSums(max) +func counterexempl(max uint, nGoRoutines uint) bool { + sums := getSums(max) - if nGoRoutines > 1 { - channels := make([](chan bool), nGoRoutines) + if nGoRoutines > 1 { + channels := make([](chan bool), nGoRoutines) - // Compute the result of each sub-range - for i := uint(0); i < nGoRoutines; i++ { - channels[i] = make(chan bool) - it := iter{uint(i), max, nGoRoutines} + // Compute the result of each sub-range + for i := uint(0); i < nGoRoutines; i++ { + channels[i] = make(chan bool) + it := iter{ uint(i), max, nGoRoutines } - go counterIter(it, &sums, channels[i]) - } + go counterexemplRange(it, &sums, channels[i]) + } - // Listen for the computation to finish - for _, c := range channels { - if msg := <-c; msg { - return true - } - } - } else { - c := make(chan bool) - it := iter{0, max, 1} + // Listen for the computation to finish + for _, c := range channels { + if msg := <-c; msg { + return true + } + } + } else { + c := make(chan bool) + it := iter{0, max, 1} - go counterIter(it, &sums, c) + go counterexemplRange(it, &sums, c) - return <-c - } + return <-c + } - return false + return false } -func counterIter(it iter, sums *[]int, c chan bool) { - for a := it.Start; a <= it.End; a += it.Step { - for b := a; b <= it.End; b++ { - diff := (*sums)[a+b] - (*sums)[a] - (*sums)[b] +func counterexemplRange(it iter, sums *[]int, c chan bool) { + for a := it.Start; a <= it.End; a += it.Step { + for b := a; b <= it.End; b++ { + diff := (*sums)[a+b] - (*sums)[a] - (*sums)[b] - if diff%9 != 0 { - c <- true - close(c) + if diff%9 != 0 { + c <- true + close(c) - return - } - } - } + return + } + } + } - c <- false - close(c) + c <- false + close(c) } func getSums(max uint) []int { - maxRange := 2 * max + 1 - sums := make([]int, maxRange) + maxRange := 2 * max + 1 + sums := make([]int, maxRange) - for i := range sums { - sums[i] = sumDigits(uint(i)) - } + for i := range sums { + sums[i] = sumDigits(uint(i)) + } - return sums + return sums } func sumDigits(n uint) int { - var sum uint + var sum uint - for { - if n <= 0 { - return int(sum) - } + for { + if n <= 0 { + return int(sum) + } - sum += n % 10 - n /= 10 - } + sum += n % 10 + n /= 10 + } }
diff --git a/README.md b/README.md @@ -29,11 +29,11 @@ avaliated as the following _(*)_: |Language |Milliseconds|Processes| |--------------|------------|---------| -|**Go** |4.1073 |4 | |**C** |15.441 |4 | |**Rust** |42.508 |4 | |**Kotlin** |12480 |1 | |**OCaml** |30448 |1 | +|**Go** |70459 |4 | |**Haskell** |121000 |1 | _* not all implementations were benchmarked_