casacore
|
#include <Primes.h>
Static Public Member Functions | |
static Bool | isPrime (uInt number) |
This function takes number and returns "True" if number is prime, "False" if it is not. More... | |
static uInt | aLargerPrimeThan (uInt number) |
This function returns the closest integer larger than number from the table of primes. More... | |
static uInt | nextLargerPrimeThan (uInt number) |
This function finds the next largest prime than number, returns that value and stores it in the table of primes. More... | |
static uInt | smallestPrimeFactor (uInt number) |
This function returns the smallest factor of number. More... | |
static Block< uInt > | factor (uInt number) |
This function returns a block, of variable length, with each factor indexed. More... | |
Static Private Member Functions | |
static void | initializeCache () |
This function returns the number of primes stored in the primes table. More... | |
Static Private Attributes | |
static Block< uInt > | cacheTable |
This is the table which stores the prime numbers. More... | |
static std::mutex | theirMutex |
Creates a reference table of prime numbers, and some functions
Prime has its usual definition (a number whose only factors are itself and one.) Zero and one are not considered to be prime.
The primary function of the Primes class is to create and maintain a list of prime numbers. This class has no constructors; all member functions are therefore declared static. The class maintains an internal cache table of prime numbers. There are also several member functions of more general use, which do not access the cached table.
The table is initialized to contain the next prime greater than each power of two. The function "aLargerPrimeThan" accepts a number and returns a larger prime number from the table of prime numbers. The function "nextLargerPrimeThan" finds the next greater integer that is a prime, returns it, and inserts it into the table. There are also functions to initialize and examine the table.
The basic prime number operations are done in three functions: "isPrime" determines whether a given number is a prime; "smallestPrimeFactor" finds the smallest factor of a given number; and "factor" returns a Block<uInt> containing a number's factors.
This class was conceived during the coding of a class that creates hash tables. The hash table class works best with a table whose size is prime. It uses the Primes class's function "nextLargerPrimeThan" to find a prime number that is close to an appropriate size. This prime number becomes the size of the hash table.
This function returns the closest integer larger than number from the table of primes.
If there is no entry in the table of primes which is larger than number, a zero is returned.
This function returns a block, of variable length, with each factor indexed.
For example, if number equaled 4, then the return block would have a length of two, and have a two stored in each cell. One and zero are special cases; this function returns a one-cell block which holds one or zero, respectively.
|
staticprivate |
This function returns the number of primes stored in the primes table.
static uInt nCachedPrimes() { return cacheTable.nelements(); }
This function returns the table of prime numbers. static Block<uInt> cachedPrimes() { return cacheTable; } This function resets the table of prime numbers to contain 31 prime numbers to avoid consuming too much memory.
This function takes number and returns "True" if number is prime, "False" if it is not.
This function finds the next largest prime than number, returns that value and stores it in the table of primes.
This function returns the smallest factor of number.