-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joshua Rubin
committed
Jun 17, 2015
1 parent
fba4c27
commit 774d9ff
Showing
8 changed files
with
117 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package primes | ||
|
||
// Sundaram is a Sieve that is calculated using the sieve of sundaram algorithm | ||
type Sundaram struct { | ||
BitSet | ||
} | ||
|
||
// NewSundaram returns a new Sundaram calculated for all values from 0 to the | ||
// nearest byte greater than n | ||
func NewSundaram(n uint64) Sieve { | ||
s := NewBitSet(n) | ||
|
||
imax := sqrt(s.Max()) | ||
for i := uint64(1); i <= imax; i++ { | ||
jmax := (n - i) / (1 + 2*i) | ||
for j := i; j <= jmax; j++ { | ||
s.Set(i + j + 2*i*j) | ||
} | ||
} | ||
|
||
ret := Sundaram{NewBitSet(n).Set(2).Set(3)} | ||
imax = (s.Max() - 1) / 2 | ||
for i := uint64(2); i <= imax; i++ { | ||
if !s.IsSet(i) { | ||
ret.Set(2*i + 1) | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
// IsPrime returns if value is a prime | ||
func (s Sundaram) IsPrime(i uint64) bool { | ||
return s.IsSet(i) | ||
} | ||
|
||
// ListPrimes returns the set of all primes in the sieve | ||
func (s Sundaram) ListPrimes() []uint64 { | ||
return s.ListSet() | ||
} | ||
|
||
// Len returns the size of the BitSet in bytes | ||
func (s Sundaram) Len() uint64 { | ||
// twice the bitset size since 2 were required | ||
return s.BitSet.Len() * 2 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package primes | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestSundaram(t *testing.T) { | ||
Convey("Sundaram algorithm should work", t, func() { | ||
max := uint64(10000) | ||
s := NewSundaram(max) | ||
So(s.Len(), ShouldEqual, 2502) | ||
numPrimes := 0 | ||
for i := uint64(0); i <= max; i++ { | ||
if s.IsPrime(i) { | ||
numPrimes++ | ||
So(isPrime(i), ShouldBeTrue) | ||
} else { | ||
So(isPrime(i), ShouldBeFalse) | ||
} | ||
} | ||
So(numPrimes, ShouldEqual, 1229) | ||
ps := s.ListPrimes() | ||
So(len(ps), ShouldEqual, 1230) | ||
for _, p := range ps { | ||
So(isPrime(p), ShouldBeTrue) | ||
} | ||
}) | ||
} |