From cb58fbfd5496e20bf5393e8d73709680eb553771 Mon Sep 17 00:00:00 2001 From: Ellen Date: Thu, 6 Feb 2020 15:56:31 +0100 Subject: [PATCH] initial commit --- main.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..9b3f6d2 --- /dev/null +++ b/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "runtime" +) + +func main() { + numThreads := runtime.NumCPU() + fmt.Printf("I have : %d threads\n", numThreads) + ch := make(chan int) + for i := 0; i < numThreads; i++ { + go sumOf3n5Multiples(1000, numThreads, 3+i, ch) + } + j := 0 + for i := 0; i < 8; i++ { + j += <-ch + + } + fmt.Printf("%d", j) +} + +func sumOf3n5Multiples(maxNumber int, stepSize int, start int, ch chan int) { + var sum = 0 + for i := start; i < maxNumber; i += stepSize { + if i%3 == 0 || i%5 == 0 { + sum += i + } + } + ch <- sum +}