Skip to content

Instantly share code, notes, and snippets.

@andygarfield
Created September 18, 2017 23:11
Show Gist options
  • Save andygarfield/78c60d2e5037f9f2dba4ed2d998a6257 to your computer and use it in GitHub Desktop.
Save andygarfield/78c60d2e5037f9f2dba4ed2d998a6257 to your computer and use it in GitHub Desktop.
Prime number generator in go
package main
import (
"fmt"
)
func checkPrime(i int, c chan int) {
prime := true
for j := 3; j < i/2; j += 2 {
if i%j == 0 {
prime = false
break
}
}
if prime == true {
c <- i
}
}
func primeRange(start, stop int, c chan int) {
for i := start; i < stop; i += 2 {
if i%5 != 0 {
go checkPrime(i, c)
}
}
}
func main() {
startNum := 1
stopNum := 100000
if startNum%2 == 0 {
startNum += 1
}
num := make(chan int)
go func() {
for {
select{
case <-num:
msg := <-num
println(msg)
}
}
}()
primeRange(startNum, stopNum, num)
// Only to keep the program running
var input string
fmt.Scanln(&input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment