From 13091c74a8e67e8e7b6006773feeeeaede600745 Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Thu, 18 Jul 2024 23:07:46 +0200 Subject: [PATCH 1/2] Add: script to calculate fibonacci numbers multi threaded --- multi-threading/fibonacci-multi-threaded.go | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 multi-threading/fibonacci-multi-threaded.go diff --git a/multi-threading/fibonacci-multi-threaded.go b/multi-threading/fibonacci-multi-threaded.go new file mode 100644 index 0000000..c4432f9 --- /dev/null +++ b/multi-threading/fibonacci-multi-threaded.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "sync" + "time" +) + +// Task struct representing a task to be processed +type Task struct { + id int + n int +} + +// Function to calculate the nth Fibonacci number (inefficient recursive approach) +func fibonacci(n int) int { + if n <= 1 { + return n + } + return fibonacci(n-1) + fibonacci(n-2) +} + +// Worker function that processes tasks from the task channel +func worker(id int, tasks <-chan Task, wg *sync.WaitGroup) { + defer wg.Done() + for task := range tasks { + start := time.Now() + result := fibonacci(task.n) + duration := time.Since(start) + fmt.Printf("Worker %d processed task %d: Fibonacci(%d) = %d (took %s)\n", + id, task.id, task.n, result, duration) + } +} + +func main() { + const numWorkers = 3 + const numTasks = 10 + + // Create a channel to send tasks to workers + tasks := make(chan Task, numTasks) + + // Create a WaitGroup to wait for all workers to finish + var wg sync.WaitGroup + + // Start worker goroutines + for i := 1; i <= numWorkers; i++ { + wg.Add(1) + go worker(i, tasks, &wg) + } + + // Send tasks to the task channel + for i := 1; i <= numTasks; i++ { + n := rand.Intn(maxFib - minFib) + tasks <- Task{id: i, n: 30} // n=30 is computationally intensive for Fibonacci + } + + // Close the task channel to signal no more tasks + close(tasks) + + // Wait for all workers to finish + wg.Wait() + fmt.Println("All tasks processed") +} From a54ddfe5f3d0c83b1fdafdfa0cdbdc741794e9b6 Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Thu, 18 Jul 2024 23:18:46 +0200 Subject: [PATCH 2/2] Fix: instead of 30 use random number between 5 and 50 --- multi-threading/fibonacci-multi-threaded.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/multi-threading/fibonacci-multi-threaded.go b/multi-threading/fibonacci-multi-threaded.go index c4432f9..c3b86b4 100644 --- a/multi-threading/fibonacci-multi-threaded.go +++ b/multi-threading/fibonacci-multi-threaded.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "math/rand" "sync" "time" ) @@ -35,6 +36,12 @@ func worker(id int, tasks <-chan Task, wg *sync.WaitGroup) { func main() { const numWorkers = 3 const numTasks = 10 + // Max and Min Fibonacci numbers + const maxFib = 50 + const minFib = 5 + + // Seed the random number generator + rand.Seed(time.Now().UnixNano()) // Create a channel to send tasks to workers tasks := make(chan Task, numTasks) @@ -54,6 +61,8 @@ func main() { tasks <- Task{id: i, n: 30} // n=30 is computationally intensive for Fibonacci } + n := rand.Intn(maxFib - minFib) // Random number between minFib and maxFib + tasks <- Task{id: i, n: n} // Close the task channel to signal no more tasks close(tasks)