How to resolve the algorithm Sequence of primes by trial division step by step in the Go programming language
How to resolve the algorithm Sequence of primes by trial division step by step in the Go programming language
Table of Contents
Problem Statement
Generate a sequence of primes by means of trial division.
Trial division is an algorithm where a candidate number is tested for being a prime by trying to divide it by other numbers. You may use primes, or any numbers of your choosing, as long as the result is indeed a sequence of primes. The sequence may be bounded (i.e. up to some limit), unbounded, starting from the start (i.e. 2) or above some given value. Organize your function as you wish, in particular, it might resemble a filtering operation, or a sieving operation. If you want to use a ready-made is_prime function, use one from the Primality by trial division page (i.e., add yours there if it isn't there already).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sequence of primes by trial division step by step in the Go programming language
==> Notes for the first code...
- A prime number is a number that is only divisible by 1 and itself.
- The Sieve of Eratosthenes is an algorithm for finding all the prime numbers up to a given limit.
- The algorithm works by iteratively marking as composite (not prime) the multiples of each prime number, starting with the first prime number, 2.
- The algorithm continues until all the numbers up to the given limit have been marked.
- The numbers that have not been marked are the prime numbers.
--> Implementation...
- The Go code you provided implements the Sieve of Eratosthenes algorithm using goroutines and channels.
- The
main
function creates a channel calledch
and starts a goroutine that calls theprimes
function. - The
primes
function sends the number 2 to thech
channel and then starts a goroutine that calls theSieve
function. - The
Sieve
function generates prime numbers and sends them to thech
channel. - The
main
function prints the first 20 prime numbers from thech
channel.
--> Notes for the second code...
- This code also generates prime numbers, but it uses a different algorithm than the Sieve of Eratosthenes.
- The algorithm used in this code is called the trial division algorithm.
- The trial division algorithm works by dividing the candidate number by all the numbers from 2 to the square root of the candidate number.
- If the candidate number is not divisible by any of these numbers, then it is a prime number.
--> Implementation...
- The Go code you provided implements the trial division algorithm using a closure.
- The
newP
function creates a new closure that generates prime numbers. - The closure maintains a state variable called
n
that represents the next prime number to be generated. - The closure also contains a loop that iterates over all the numbers from 2 to the square root of
n
. - If
n
is divisible by any of these numbers, then the closure continues to the next number. - If
n
is not divisible by any of these numbers, then the closure returnsn
as the next prime number. - The
main
function calls thenewP
function to create a new closure that generates prime numbers. - The
main
function then prints the first 20 prime numbers generated by the closure.
Source code in the go programming language
package main
import "fmt"
func NumsFromBy(from int, by int, ch chan<- int) {
for i := from; ; i+=by {
ch <- i
}
}
func Filter(in <-chan int, out chan<- int, prime int) {
for {
i := <-in
if i%prime != 0 { // here is the trial division
out <- i
}
}
}
func Sieve(out chan<- int) {
out <- 3
q := 9
ps := make(chan int)
go Sieve(ps) // separate primes supply
p := <-ps
nums := make(chan int)
go NumsFromBy(5,2,nums) // end of setup
for i := 0; ; i++ {
n := <-nums
if n < q {
out <- n // n is prime
} else {
ch1 := make(chan int) // n == q == p*p
go Filter(nums, ch1, p) // creation of a filter by p, at p*p
nums = ch1
p = <-ps // next prime
q = p*p // and its square
}
}
}
func primes (c chan<- int) {
c <- 2
go Sieve(c)
}
func main() {
ch := make(chan int)
go primes(ch)
fmt.Print("First twenty:")
for i := 0; i < 20; i++ {
fmt.Print(" ", <-ch)
}
fmt.Println()
}
package main
import "fmt"
func newP() func() int {
n := 1
return func() int {
for {
n++
// Trial division as naïvely as possible. For a candidate n,
// numbers between 1 and n are checked to see if they divide n.
// If no number divides n, n is prime.
for f := 2; ; f++ {
if f == n {
return n
}
if n%f == 0 { // here is the trial division
break
}
}
}
}
}
func main() {
p := newP()
fmt.Print("First twenty:")
for i := 0; i < 20; i++ {
fmt.Print(" ", p())
}
fmt.Println()
}
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Action! programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Beef programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the Ruby programming language
You may also check:How to resolve the algorithm Percolation/Mean run density step by step in the Fortran programming language