- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to compute all prime numbers up to a given number using concurrency
In this Go language article, we will write programs to compute all prime numbers up to a given number using concurrent execution. Concurrent execution is the process of executing multiple tasks simultaneously. In Golang, go routines and channels are used to create concurrent programs.
Go routines are lightweight threads which are managed by the Go runtime and channels help in the communication between Go routines without any conflicts.
Syntax
func make ([] type, size, capacity)
The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.
func range(variable)
The range function is used to iterate over any data type. To use this, we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element of the variable.
Algorithm
This program imports main, fmt and sync package in the program
Create a main function
In the main set the range with the variable limit up to which the prime numbers will be calculated
Then, create a channel named primes using make function which will obtain the prime numbers
In this step, create a Wait Group wg so that all go routines finish before exiting the main function
Then, start the go routine by calling the generate Primes function
Here, pass the limit, primes channel, and the wg as arguments
Start another go routine to print the prime numbers by calling the print Primes function
Here, pass the primes channel and the Wait Group as arguments
Then, use wait function to wait for all go routines to finish
In this step, create a function generate Primes to generate prime numbers up to a given limit
In this function, send the initial prime numbers 2 and 3 to the primes channel. Then, it iterates from 5 up to the limit in steps of 2
In each iteration, it checks for primality by iterating from 3 up to the square root of the number
If the number is divisible by any of these divisors, it's not a prime, and is Prime is set to false. Otherwise, it remains true
If the number is prime it is sent to the primes channel
Create print Primes function to receives the prime numbers from the primes channel and print them
Once all numbers are printed, it calls wg.Done() to signal the Wait Group that it has finished printing
The program ends after all go routines finish
Example
In this example, we will write a Golang program to compute all prime numbers using go routines and channels so that concurrent execution takes place.
package main import ( "fmt" "sync" ) func main() { limit := 100 primes := make(chan int) var wg sync.WaitGroup wg.Add(1) go generatePrimes(limit, primes, &wg) wg.Add(1) go printPrimes(primes, &wg) wg.Wait() } func generatePrimes(limit int, primes chan<- int, wg *sync.WaitGroup) { defer close(primes) defer wg.Done() primes <- 2 primes <- 3 for num := 5; num <= limit; num += 2 { isPrime := true for i := 3; i*i <= num; i += 2 { if num%i == 0 { isPrime = false break } } if isPrime { primes <- num } } } func printPrimes(primes <-chan int, wg *sync.WaitGroup) { defer wg.Done() fmt.Println("The prime numbers till the rage of 100 are:") for prime := range primes { fmt.Println(prime) } }
Output
The prime numbers till the rage of 100 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Conclusion
We compiled and executed the program of computing prime numbers up to a given number with the help of an example in which we used go routines and channels for concurrent implementation.