Primes.h

Classes

Primes -- Creates a reference table of prime numbers, and some functions (full description)

class Primes

Interface

Public Members
static Bool isPrime(uInt number)
static uInt aLargerPrimeThan(uInt number)
static uInt nextLargerPrimeThan(uInt number)
static uInt smallestPrimeFactor(uInt number)
static Block<uInt> factor(uInt number)
static uInt nCachedPrimes()
static void initializeCache()
static Block<uInt> cachedPrimes()

Description

Review Status

Reviewed By:
Gareth Hunt
Date Reviewed:
94/08/19
Programs:
Tests:

Prerequisite

Etymology

Prime has its usual definition (a number whose only factors are itself and one.) Zero and one are not considered to be prime.

Synopsis

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 containing a number's factors.

Example

    #include <scimath/Mathematics/Primes.h>
    #include <casa/Utilities/Assert.h>
    #include <iostream>
    
    // Refer also to tPrimes.cc
    
    int main() {
        Block<uInt> BB, DD;
        uInt C, i;
        if(! Primes::isPrime(4)) {                         //if this number 
            cout<<"Four is not a prime number"<<endl;      //is not prime
            BB = Primes::factor(4);                        //than factor it
    
            if( BB[0] != Primes::smallestPrimeFactor(4) ){ //check that first 
                                                           //factor equals
                 cerr<<"something is wrong"<<endl;         //the smallest 
            }
            cout<<"4 factored:"<<endl;
            for ( i = 0; i < BB.nelements(); i++ ) {
                cout<<BB[i]<<endl;
            }
    
            C = Primes::aLargerPrimeThan(4);  
            if ( (C-5) > 4 ) {                         //if difference is more 
                                                       //than five, then
                C = Primes::nextLargerPrimeThan(4);    //find next lprime
            } 
            DebugAssertExit( C == Primes::aLargerPrimeThan(4)); //now the prime resides
        }                                              //in the cache table 
        if( Primes::nCachedPrimes() > 50 ) {
            Primes::initializeCache();
        }
        DD = Primes::cachedPrimes();
        cout<<"The Table of Primes"<<endl;
        for ( i = 0; i < Primes::nCachedPrimes(); i++ ) {
            cout<<DD[i]<<endl;
        }
        return 0;
    }
    
    

Motivation

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.

To Do