diff --git a/Makefile b/Makefile index 809f603..f2a8f97 100644 --- a/Makefile +++ b/Makefile @@ -12,15 +12,15 @@ endif all: build lint: - golint ./... - go vet ./... + @golint ./... | grep -v '^sievealgo_string.go:' || true + @go vet ./... -test: +test: sievealgo_string.go godep go test -v -race ./... coverage: .acc.out -sievealgo_string.go: sieve.go +sievealgo_string.go: sievealgo.go go generate .acc.out: $(GO_FILES) sievealgo_string.go diff --git a/cmd/primes/main.go b/cmd/primes/main.go index cc5517a..f77080a 100644 --- a/cmd/primes/main.go +++ b/cmd/primes/main.go @@ -56,7 +56,6 @@ func before(c *cli.Context) error { algopt := c.GlobalString("algorithm") switch algopt { case "eratosthenes", "": - fmt.Fprintf(os.Stderr, "using sieve of eratosthenes algorithm\n") algo = primes.EratosthenesAlgo default: cli.ShowAppHelp(c) diff --git a/primes.go b/primes.go index 0a08eca..fa83384 100644 --- a/primes.go +++ b/primes.go @@ -34,12 +34,13 @@ func Between(a, b uint64, algo SieveAlgo) []uint64 { } } - fmt.Fprintf(os.Stderr, "found %s primes between %s and %s (inclusive) in %v using %s of memory\n", + fmt.Fprintf(os.Stderr, "found %s primes between %s and %s (inclusive) in %v using %s of memory using %s\n", humanize.Comma(int64(len(ret))), humanize.Comma(int64(a)), humanize.Comma(int64(b)), time.Now().Sub(start), - humanize.Bytes(s.Len())) + humanize.Bytes(s.Len()), + algo) return ret } diff --git a/primes_test.go b/primes_test.go index 68f53e6..f913190 100644 --- a/primes_test.go +++ b/primes_test.go @@ -7,10 +7,6 @@ import ( ) func TestPrimes(t *testing.T) { - algos := []SieveAlgo{ - EratosthenesAlgo, - } - max := uint64(10000) Convey("Between should work", t, func() { @@ -19,7 +15,14 @@ func TestPrimes(t *testing.T) { So(primes, ShouldResemble, []uint64{}) }) - for _, algo := range algos { + Loop: + for _, algo := range SieveAlgos { + switch algo { + case SundaramAlgo, AtkinAlgo: + Print("TODO(jrubin) enable prime test for ", algo, "\n") + continue Loop + } + primes := Between(max, 0, algo) So(len(primes), ShouldEqual, 1229) for _, val := range primes { diff --git a/sievealgo.go b/sievealgo.go index 5865abe..f6a1b9d 100644 --- a/sievealgo.go +++ b/sievealgo.go @@ -6,6 +6,24 @@ type SieveAlgo int const ( // EratosthenesAlgo is the sieve of eratosthene algorithm EratosthenesAlgo SieveAlgo = iota + + // SundaramAlgo is the sieve of sundaram algorithm + SundaramAlgo + + // AtkinAlgo is the sieve of atkin algorithm + AtkinAlgo +) + +var ( + SieveAlgos []SieveAlgo ) +func init() { + SieveAlgos = make([]SieveAlgo, 3) + + SieveAlgos[EratosthenesAlgo] = EratosthenesAlgo + SieveAlgos[SundaramAlgo] = SundaramAlgo + SieveAlgos[AtkinAlgo] = AtkinAlgo +} + //go:generate stringer -type=SieveAlgo diff --git a/sievealgo_string.go b/sievealgo_string.go index 71030c7..be07e6c 100644 --- a/sievealgo_string.go +++ b/sievealgo_string.go @@ -4,9 +4,9 @@ package primes import "fmt" -const _SieveAlgo_name = "EratosthenesAlgo" +const _SieveAlgo_name = "EratosthenesAlgoSundaramAlgoAtkinAlgo" -var _SieveAlgo_index = [...]uint8{0, 16} +var _SieveAlgo_index = [...]uint8{0, 16, 28, 37} func (i SieveAlgo) String() string { if i < 0 || i+1 >= SieveAlgo(len(_SieveAlgo_index)) { diff --git a/sievealgo_test.go b/sievealgo_test.go index 1a4622a..208d2a7 100644 --- a/sievealgo_test.go +++ b/sievealgo_test.go @@ -8,7 +8,19 @@ import ( func TestSieveAlgo(t *testing.T) { Convey("SieveAlgos should be correct", t, func() { + So(len(SieveAlgos), ShouldEqual, 3) + for i, algo := range SieveAlgos { + So(SieveAlgos[i], ShouldEqual, i) + So(algo, ShouldEqual, i) + So(algo.String(), ShouldEqual, SieveAlgo(i).String()) + } + So(EratosthenesAlgo, ShouldEqual, 0) + So(SundaramAlgo, ShouldEqual, 1) + So(AtkinAlgo, ShouldEqual, 2) + So(EratosthenesAlgo.String(), ShouldEqual, "EratosthenesAlgo") + So(SundaramAlgo.String(), ShouldEqual, "SundaramAlgo") + So(AtkinAlgo.String(), ShouldEqual, "AtkinAlgo") }) }