Background: I am trying to implement a logic which find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20. I have implemented a sequential version and got an answer as 232792560.
Question: When I try to build some concurrency on this problem ( see uncommented block of code), it does run but never shows any result. Can anyone of you guide me on where I am going wrong?
Note: I am very much new to golang; and I am aware that, this is not the best problem for concurrency since there is no guarantee that I will have the smallest positive number as first result. However, I tried it out of my curiosity.
package main
import(
"fmt"
)
func divide(num int) bool {
for i := 1; i <= 20; i++ {
if num % i != 0 {
return false
}
}
return true
}
func main() {
num:=0
//simple function
/*for {
num++;
result := divide(num)
if result {
fmt.Println("Smallest number is: ", num)
break
}
}*/
//go-routine
//go-routine
var wg sync.WaitGroup
for {
num++;
wg.Add(1)
go func(x int) {
result := divide(x)
if result {
fmt.Println("Smallest number is: ", x)
defer wg.Done()
}
}(num)
}
wg.Wait()
fmt.Println("End.")
}