casacore
Loading...
Searching...
No Matches
Primes.h
Go to the documentation of this file.
1//# Primes.h: This class provides some prime number operations using a cached table
2//# Copyright (C) 1994,1995,1999,2001
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef SCIMATH_PRIMES_H
27#define SCIMATH_PRIMES_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/Containers/Block.h>
31
32#include <mutex>
33
34namespace casacore { //# NAMESPACE CASACORE - BEGIN
35
36// <summary> Creates a reference table of prime numbers, and some functions </summary>
37//
38// <reviewed reviewer="Gareth Hunt" date="94/08/19" tests="tPrimes">
39//
40// <prerequisite>
41// <li>Understanding Block is only peripherally important.
42// </prerequisite>
43//
44// <etymology>
45// Prime has its usual definition (a number whose only factors are
46// itself and one.) Zero and one are not considered to be prime.
47// </etymology>
48//
49// <synopsis>
50// The primary function of the Primes class is to create and maintain a list of
51// prime numbers. This class has no constructors; all member functions are
52// therefore declared static. The class maintains an internal cache table of
53// prime numbers. There are also several member functions of more general use,
54// which do not access the cached table.
55//
56// The table is initialized to contain the next prime greater than each power
57// of two. The function "aLargerPrimeThan" accepts a number and returns a
58// larger prime number from the table of prime numbers. The function
59// "nextLargerPrimeThan" finds the next greater integer that is a prime,
60// returns it, and inserts it into the table. There are also functions to
61// initialize and examine the table.
62//
63// The basic prime number operations are done in three functions: "isPrime"
64// determines whether a given number is a prime; "smallestPrimeFactor" finds
65// the smallest factor of a given number; and "factor" returns a Block<uInt>
66// containing a number's factors.
67// </synopsis>
68//
69// <example>
70// <srcblock>
71// #include <casacore/scimath/Mathematics/Primes.h>
72// #include <casacore/casa/Utilities/Assert.h>
73// #include <iostream>
74//
75// // Refer also to tPrimes.cc
76//
77// int main() {
78// Block<uInt> BB, DD;
79// uInt C, i;
80// if(! Primes::isPrime(4)) { //if this number
81// cout<<"Four is not a prime number"<<endl; //is not prime
82// BB = Primes::factor(4); //than factor it
83//
84// if( BB[0] != Primes::smallestPrimeFactor(4) ){ //check that first
85// //factor equals
86// cerr<<"something is wrong"<<endl; //the smallest
87// }
88// cout<<"4 factored:"<<endl;
89// for ( i = 0; i < BB.nelements(); i++ ) {
90// cout<<BB[i]<<endl;
91// }
92//
93// C = Primes::aLargerPrimeThan(4);
94// if ( (C-5) > 4 ) { //if difference is more
95// //than five, then
96// C = Primes::nextLargerPrimeThan(4); //find next lprime
97// }
98// DebugAssertExit( C == Primes::aLargerPrimeThan(4)); //now the prime resides
99// } //in the cache table
100// if( Primes::nCachedPrimes() > 50 ) {
101// Primes::initializeCache();
102// }
103// DD = Primes::cachedPrimes();
104// cout<<"The Table of Primes"<<endl;
105// for ( i = 0; i < Primes::nCachedPrimes(); i++ ) {
106// cout<<DD[i]<<endl;
107// }
108// return 0;
109// }
110//
111// </srcblock>
112// </example>
113//
114// <motivation>
115// This class was conceived during the coding of a class that creates hash
116// tables. The hash table class works best with a table whose size is prime.
117// It uses the Primes class's function "nextLargerPrimeThan" to find a prime
118// number that is close to an appropriate size. This prime number becomes the
119// size of the hash table.
120// </motivation>
121//
122// <todo asof="$DATE:$">
123// <li> This class should not be used to generate large sets of prime
124// numbers - it is not designed for efficiency at this.
125// The algorithm checks 2, 3, and (6n +/- 1) up to the square
126// root of the candidate prime.
127// <li> The size of the prime numbers are restricted by the size of an
128// unsigned integer (2^31-1 on 32 bit machines).
129// </todo>
130
131class Primes {
132public:
133
134 //This function takes number and returns "True" if number is prime, "False"
135 //if it is not.
136 static Bool isPrime(uInt number);
137
138 //This function returns the closest integer larger than number from the
139 //table of primes. If there is no entry in the table of primes which is
140 //larger than number, a zero is returned.
141 static uInt aLargerPrimeThan(uInt number);
142
143 //This function finds the next largest prime than number, returns that
144 //value and stores it in the table of primes.
145 static uInt nextLargerPrimeThan(uInt number); // adds to cache
146
147 //This function returns the smallest factor of number.
149
150 //This function returns a block, of variable length, with each factor
151 //indexed. For example, if number equaled 4, then the return block would
152 //have a length of two, and have a two stored in each cell. One and zero
153 //are special cases; this function returns a one-cell block which holds
154 //one or zero, respectively.
155 static Block<uInt> factor(uInt number);
156
157 // This function returns the number of primes stored in the primes table.
158 // static uInt nCachedPrimes()
159 // { return cacheTable.nelements(); }
160
161 //This function returns the table of prime numbers.
162 //static Block<uInt> cachedPrimes()
163 // { return cacheTable; }
164
165private:
166
167 //This function resets the table of prime numbers to contain 31 prime
168 //numbers to avoid consuming too much memory.
169 static void initializeCache();
170
171 //This is the table which stores the prime numbers.
173 static std::mutex theirMutex;
174};
175
176
177} //# NAMESPACE CASACORE - END
178
179#endif
simple 1-D array
Definition Block.h:198
static Block< uInt > factor(uInt number)
This function returns a block, of variable length, with each factor indexed.
static uInt aLargerPrimeThan(uInt number)
This function returns the closest integer larger than number from the table of primes.
static void initializeCache()
This function returns the number of primes stored in the primes table.
static uInt smallestPrimeFactor(uInt number)
This function returns the smallest factor of number.
static Block< uInt > cacheTable
This is the table which stores the prime numbers.
Definition Primes.h:172
static std::mutex theirMutex
Definition Primes.h:173
static uInt nextLargerPrimeThan(uInt number)
This function finds the next largest prime than number, returns that value and stores it in the table...
static Bool isPrime(uInt number)
This function takes number and returns "True" if number is prime, "False" if it is not.
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40