Skip to content

Commit

Permalink
start stubbing out other sieve algos
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Rubin committed Jun 17, 2015
1 parent 10f83ba commit 88a4340
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion cmd/primes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions primes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 8 additions & 5 deletions primes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
)

func TestPrimes(t *testing.T) {
algos := []SieveAlgo{
EratosthenesAlgo,
}

max := uint64(10000)

Convey("Between should work", t, func() {
Expand All @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions sievealgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions sievealgo_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
12 changes: 12 additions & 0 deletions sievealgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}

0 comments on commit 88a4340

Please sign in to comment.