casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: aips2-request@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 //# $Id$
27 
28 #ifndef SCIMATH_PRIMES_H
29 #define SCIMATH_PRIMES_H
30 
31 #include <casacore/casa/aips.h>
33 
34 #include <mutex>
35 
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38 // <summary> Creates a reference table of prime numbers, and some functions </summary>
39 //
40 // <reviewed reviewer="Gareth Hunt" date="94/08/19" tests="tPrimes">
41 //
42 // <prerequisite>
43 // <li>Understanding Block is only peripherally important.
44 // </prerequisite>
45 //
46 // <etymology>
47 // Prime has its usual definition (a number whose only factors are
48 // itself and one.) Zero and one are not considered to be prime.
49 // </etymology>
50 //
51 // <synopsis>
52 // The primary function of the Primes class is to create and maintain a list of
53 // prime numbers. This class has no constructors; all member functions are
54 // therefore declared static. The class maintains an internal cache table of
55 // prime numbers. There are also several member functions of more general use,
56 // which do not access the cached table.
57 //
58 // The table is initialized to contain the next prime greater than each power
59 // of two. The function "aLargerPrimeThan" accepts a number and returns a
60 // larger prime number from the table of prime numbers. The function
61 // "nextLargerPrimeThan" finds the next greater integer that is a prime,
62 // returns it, and inserts it into the table. There are also functions to
63 // initialize and examine the table.
64 //
65 // The basic prime number operations are done in three functions: "isPrime"
66 // determines whether a given number is a prime; "smallestPrimeFactor" finds
67 // the smallest factor of a given number; and "factor" returns a Block<uInt>
68 // containing a number's factors.
69 // </synopsis>
70 //
71 // <example>
72 // <srcblock>
73 // #include <casacore/scimath/Mathematics/Primes.h>
74 // #include <casacore/casa/Utilities/Assert.h>
75 // #include <iostream>
76 //
77 // // Refer also to tPrimes.cc
78 //
79 // int main() {
80 // Block<uInt> BB, DD;
81 // uInt C, i;
82 // if(! Primes::isPrime(4)) { //if this number
83 // cout<<"Four is not a prime number"<<endl; //is not prime
84 // BB = Primes::factor(4); //than factor it
85 //
86 // if( BB[0] != Primes::smallestPrimeFactor(4) ){ //check that first
87 // //factor equals
88 // cerr<<"something is wrong"<<endl; //the smallest
89 // }
90 // cout<<"4 factored:"<<endl;
91 // for ( i = 0; i < BB.nelements(); i++ ) {
92 // cout<<BB[i]<<endl;
93 // }
94 //
95 // C = Primes::aLargerPrimeThan(4);
96 // if ( (C-5) > 4 ) { //if difference is more
97 // //than five, then
98 // C = Primes::nextLargerPrimeThan(4); //find next lprime
99 // }
100 // DebugAssertExit( C == Primes::aLargerPrimeThan(4)); //now the prime resides
101 // } //in the cache table
102 // if( Primes::nCachedPrimes() > 50 ) {
103 // Primes::initializeCache();
104 // }
105 // DD = Primes::cachedPrimes();
106 // cout<<"The Table of Primes"<<endl;
107 // for ( i = 0; i < Primes::nCachedPrimes(); i++ ) {
108 // cout<<DD[i]<<endl;
109 // }
110 // return 0;
111 // }
112 //
113 // </srcblock>
114 // </example>
115 //
116 // <motivation>
117 // This class was conceived during the coding of a class that creates hash
118 // tables. The hash table class works best with a table whose size is prime.
119 // It uses the Primes class's function "nextLargerPrimeThan" to find a prime
120 // number that is close to an appropriate size. This prime number becomes the
121 // size of the hash table.
122 // </motivation>
123 //
124 // <todo asof="$DATE:$">
125 // <li> This class should not be used to generate large sets of prime
126 // numbers - it is not designed for efficiency at this.
127 // The algorithm checks 2, 3, and (6n +/- 1) up to the square
128 // root of the candidate prime.
129 // <li> The size of the prime numbers are restricted by the size of an
130 // unsigned integer (2^31-1 on 32 bit machines).
131 // </todo>
132 
133 class Primes {
134 public:
135 
136  //This function takes number and returns "True" if number is prime, "False"
137  //if it is not.
138  static Bool isPrime(uInt number);
139 
140  //This function returns the closest integer larger than number from the
141  //table of primes. If there is no entry in the table of primes which is
142  //larger than number, a zero is returned.
143  static uInt aLargerPrimeThan(uInt number);
144 
145  //This function finds the next largest prime than number, returns that
146  //value and stores it in the table of primes.
147  static uInt nextLargerPrimeThan(uInt number); // adds to cache
148 
149  //This function returns the smallest factor of number.
150  static uInt smallestPrimeFactor(uInt number);
151 
152  //This function returns a block, of variable length, with each factor
153  //indexed. For example, if number equaled 4, then the return block would
154  //have a length of two, and have a two stored in each cell. One and zero
155  //are special cases; this function returns a one-cell block which holds
156  //one or zero, respectively.
157  static Block<uInt> factor(uInt number);
158 
159  // This function returns the number of primes stored in the primes table.
160  // static uInt nCachedPrimes()
161  // { return cacheTable.nelements(); }
162 
163  //This function returns the table of prime numbers.
164  //static Block<uInt> cachedPrimes()
165  // { return cacheTable; }
166 
167 private:
168 
169  //This function resets the table of prime numbers to contain 31 prime
170  //numbers to avoid consuming too much memory.
171  static void initializeCache();
172 
173  //This is the table which stores the prime numbers.
175  static std::mutex theirMutex;
176 };
177 
178 
179 } //# NAMESPACE CASACORE - END
180 
181 #endif
static Block< uInt > factor(uInt number)
This function returns a block, of variable length, with each factor indexed.
static void initializeCache()
This function returns the number of primes stored in the primes table.
static Block< uInt > cacheTable
This is the table which stores the prime numbers.
Definition: Primes.h:174
static Bool isPrime(uInt number)
This function takes number and returns &quot;True&quot; if number is prime, &quot;False&quot; if it is not...
static uInt smallestPrimeFactor(uInt number)
This function returns the smallest factor of number.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
static std::mutex theirMutex
Definition: Primes.h:175
static uInt nextLargerPrimeThan(uInt number)
This function finds the next largest prime than number, returns that value and stores it in the table...
Creates a reference table of prime numbers, and some functions.
Definition: Primes.h:133
static uInt aLargerPrimeThan(uInt number)
This function returns the closest integer larger than number from the table of primes.
unsigned int uInt
Definition: aipstype.h:51