sieve

package module
v0.0.0-...-b0c3028 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2022 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Overview

A package that implements the Sieve of Eratosthenes algorithm to compute prime numbers in a given range.

Index

Constants

View Source
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

func NewSieve(r int) (s *Sieve, err error)

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

func (s *Sieve) ComputePrimes() *Sieve

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

func (s *Sieve) Count() int

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

func (s *Sieve) Elapsed() time.Duration

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

func (s *Sieve) Integers() []bool

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

func (s *Sieve) PrimeMap() (pm map[int]bool)

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

func (s *Sieve) Primes() []int

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.

func (*Sieve) Range

func (s *Sieve) Range() int

Returns the range of integers that will be or have been checked for primality. This is the integer value provided to the NewSieve() method and it is the size of the boolean array returned by Integers() less one, i.e., sieve.Range() = len(sieve.Integers()) - 1.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL