diff --git a/main.go b/main.go index 8dd6ebe..b75d371 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func main() { r1 := mrand.New(s1) start := time.Now() - p, _ := prime(r1, 8096, 1000) + p, _ := prime(r1, 8096, 32) finish := time.Now() elapsed := finish.Sub(start) @@ -38,11 +38,12 @@ func main() { } func prime(rand io.Reader, bits int, threads int) (p *big.Int, err error) { - bytes := make([]byte, (bits+7)/8) + //bytes := make([]byte, (bits+7)/8) - p = new(big.Int) + //p = new(big.Int) jobs := make(chan job) results := make(chan job) + numbers := make(chan *big.Int, 100) done := new(bool) *done = false @@ -50,20 +51,13 @@ func prime(rand io.Reader, bits int, threads int) (p *big.Int, err error) { go worker(i, jobs, results, done) } + for i := 0; i < 4; i++ { + go rng(i, bits, numbers, done) + } + for i := 0; ; i++ { - _, err = io.ReadFull(rand, bytes) - if err != nil { - return nil, err - } - // This makes sure that the least significant bit is always 1 - // meaning that the number is always odd, since even numbers - // aren't prime - bytes[len(bytes)-1] |= 1 - - p.SetBytes(bytes) - newjob := &job{ - p: p, + p: <-numbers, i: 10, id: i, } @@ -82,6 +76,23 @@ func prime(rand io.Reader, bits int, threads int) (p *big.Int, err error) { } +func rng(id int, bits int, num chan<- *big.Int, done *bool) { + s1 := mrand.NewSource(time.Hour.Microseconds()) + rand := mrand.New(s1) + bytes := make([]byte, (bits+7)/8) + for !*done { + p := new(big.Int) + io.ReadFull(rand, bytes) + // This makes sure that the least significant bit is always 1 + // meaning that the number is always odd, since even numbers + // aren't prime + bytes[len(bytes)-1] |= 1 + + p.SetBytes(bytes) + num <- p + } +} + func worker(id int, jobs <-chan job, results chan<- job, done *bool) { //fmt.Printf("Worker %d starting now!\n", id) for j := range jobs {