Sieve

// sieve.go
// description: Algorithms for generating prime numbers efficiently
// author(s) [Taj](https://github.com/tjgurwara99)
// see sieve_test.go

package prime

// Generate generates the sequence of integers starting at 2 and sends it to the channel `ch`
func GenerateChannel(ch chan<- int) {
	for i := 2; ; i++ {
		ch <- i
	}
}

// Sieve Sieving the numbers that are not prime from the channel - basically removing them from the channels
func Sieve(in <-chan int, out chan<- int, prime int) {
	for {
		i := <-in
		if i%prime != 0 {
			out <- i
		}
	}
}

// Generate returns a int slice of prime numbers up to the limit
func Generate(limit int) []int {
	var primes []int

	ch := make(chan int)
	go GenerateChannel(ch)

	for i := 0; i < limit; i++ {
		primes = append(primes, <-ch)
		ch1 := make(chan int)
		go Sieve(ch, ch1, primes[i])
		ch = ch1
	}

	return primes
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.