Created
September 18, 2017 23:11
-
-
Save andygarfield/78c60d2e5037f9f2dba4ed2d998a6257 to your computer and use it in GitHub Desktop.
Prime number generator in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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