Documentation
¶
Overview ¶
A package that implements the Sieve of Eratosthenes algorithm to compute prime numbers in a given range.
Index ¶
Constants ¶
const MAX_HALF int = 10
Defines the number of primes to display around an ellipsis that is used when large numbers of primes are requested.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ISieve ¶
type ISieve interface {
NewSieve(r int) // new prime computer with range r
ComputePrimes() // compute all primes in range
PrintPrimes() // print summary of primes found in range
Integers() []bool // [0 < n < range] == true => prime number
Primes() []int // [0 < m < count] => a prime number
PrimeMap() map[int]bool // map[m] == true => a prime number
Range() int // range of integers tested for primality
Count() int // count of primes in range tested
Elapsed() time.Duration // time taken to compute primes in range
}
An interface that defines all actions associated with computing a set of prime numbers in a given range.
type Sieve ¶
type Sieve struct {
// contains filtered or unexported fields
}
A structure that contains the state of a sieve. The structure is opaque -- no fields are exported -- because fields should be accessed only via the ISieve interface methods.
func NewSieve ¶
Creates a new prime number computer with range r. Returns a pointer to a Sieve structure that may be used to call methods defined by the ISieve interface. It also returns an error that wuill be non-nil if an errort was detected.
func (*Sieve) ComputePrimes ¶
Computes all prime mumbers using the given sieve. Returns a Sieve pointer so that ISieve member functions can be chained together for convenience.
func (*Sieve) Count ¶
Returns the number of prime numbers found by the computePrimes() method. It is the size of the integer array returned by Primes().
func (*Sieve) Elapsed ¶
Returns a structure containing the time duration of the primes calculation, i.e., how long it took to complete the computePrimes() method.
func (*Sieve) Integers ¶
Returns an array of boolean values that indicate whether an array index value is a prime number or not. If called before calling the computePrimes() method, the array will contain only true values except for the zeroth and first indices which will be false; these are the setup values that computePrimes() uses in its algorithm.
func (*Sieve) PrimeMap ¶
Returns a boolean map of the prime numbers found by the computePrimes() method. The returned map will be empty before calling computePrimes().
func (*Sieve) Primes ¶
Returns an integer array containing only the prime numbers found by the computePrimes() method. The array is nil before calling computePrimes().
func (*Sieve) PrintPrimes ¶
func (s *Sieve) PrintPrimes()
Prints any previously calculated prime numbers.