Skip to content

Commit

Permalink
Merge ff0828b into e979b4d
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Apr 23, 2019
2 parents e979b4d + ff0828b commit fca1d0f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,4 @@ problems from
* [Day 240](https://github.com/vaskoz/dailycodingproblem-go/issues/492)
* [Day 241](https://github.com/vaskoz/dailycodingproblem-go/issues/493)
* [Day 242](https://github.com/vaskoz/dailycodingproblem-go/issues/498)
* [Day 244](https://github.com/vaskoz/dailycodingproblem-go/issues/503)
51 changes: 51 additions & 0 deletions day244/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package day244

import "math"

// SieveOfEratosthenes returns all the primes strictly less than n.
// If n happens to be a prime, it will NOT be returned in the result.
// Runtime is O(N log log N)
func SieveOfEratosthenes(n int) []int {
if n < 2 {
return nil
}
isNotPrime := make([]bool, n)
isNotPrime[0], isNotPrime[1] = true, true
for i := 2; i <= int(math.Sqrt(float64(n))); i++ {
if !isNotPrime[i] {
for j := i * i; j < n; j += i {
isNotPrime[j] = true
}
}
}
var primes []int
for p, notPrime := range isNotPrime {
if !notPrime {
primes = append(primes, p)
}
}
return primes
}

// SieveOfEratosthenesGenerator returns primes indefinitely.
// Only runs when the channel is read for more primes.
func SieveOfEratosthenesGenerator() <-chan int {
primes := make(chan int)
go func() {
n := 2
composites := make(map[int][]int)
for {
if _, found := composites[n]; !found {
primes <- n
composites[n*n] = []int{n}
} else {
for _, prime := range composites[n] {
composites[n+prime] = append(composites[n+prime], prime)
}
delete(composites, n)
}
n++
}
}()
return primes
}
60 changes: 60 additions & 0 deletions day244/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package day244

import (
"reflect"
"testing"
)

var testcases = []struct {
n int
primes []int
}{
{100, []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}},
{2, nil},
{1, nil},
{0, nil},
}

func TestSieveOfEratosthenes(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if primes := SieveOfEratosthenes(tc.n); !reflect.DeepEqual(primes, tc.primes) {
t.Errorf("Expected %v, got %v", tc.primes, primes)
}
}
}

func BenchmarkSieveOfEratosthenes(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
SieveOfEratosthenes(tc.n)
}
}
}

func TestSieveOfEratosthenesGenerator(t *testing.T) {
t.Parallel()
primesToThousand := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271,
277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383,
389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491,
499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613,
617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857,
859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
991, 997}
primeGenerator := SieveOfEratosthenesGenerator()
for _, prime := range primesToThousand {
if p := <-primeGenerator; p != prime {
t.Errorf("Expected %v, got %v", prime, p)
}
}
}

func BenchmarkSieveOfEratosthenesGenerator(b *testing.B) {
primeGenerator := SieveOfEratosthenesGenerator()
for i := 0; i < b.N; i++ {
<-primeGenerator
}
}

0 comments on commit fca1d0f

Please sign in to comment.