permlib 0.2.9
Library for permutation computations
prime_helper.h
1// ---------------------------------------------------------------------------
2//
3// This file is part of PermLib.
4//
5// Copyright (c) 2009-2011 Thomas Rehn <thomas@carmen76.de>
6// All rights reserved.
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions
10// are met:
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The name of the author may not be used to endorse or promote products
17// derived from this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// ---------------------------------------------------------------------------
31
32#ifndef PRIME_HELPER_H_
33#define PRIME_HELPER_H_
34
35#include <algorithm>
36#include <boost/assert.hpp>
37
38namespace permlib {
39
42public:
44 static const unsigned int largestNumberForPrimalityCheck;
45
47
52 static bool isPrimeNumber(unsigned int x, bool checkBounds) {
53 if (checkBounds && x > largestNumberForPrimalityCheck) {
54 // number too big for our simple check
55 BOOST_ASSERT( false );
56 return false;
57 }
58
60 // the number is to big for our simple check
61 return false;
62 } else if (x > largestPrime) {
63 for (unsigned int i = 0; i < numberOfPrimes; ++i) {
64 if ((x % primes[i]) == 0)
65 return false;
66 }
67 return true;
68 }
69 return std::binary_search(primes, primes+numberOfPrimes, x);
70 }
71
73 static const unsigned int* firstPrime() { return primes; }
75 static const unsigned int* lastPrime() { return primes + numberOfPrimes; }
76private:
77 static const unsigned int numberOfPrimes;
78 // This list is probably incomplete ;)
79 static const unsigned int primes[];
80 static const unsigned int largestPrime;
81};
82
83#define PRIME_HELPER_NUMBER_OF_PRIMES 170
84const unsigned int PrimeHelper::numberOfPrimes = PRIME_HELPER_NUMBER_OF_PRIMES;
85// Probably this list is incomplete ;)
86const unsigned int PrimeHelper::primes[PRIME_HELPER_NUMBER_OF_PRIMES] = {
87 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,
88 127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,
89 283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,
90 467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,
91 661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,
92 877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013
93};
94const unsigned int PrimeHelper::largestPrime = primes[numberOfPrimes-1];
95const unsigned int PrimeHelper::largestNumberForPrimalityCheck = largestPrime * largestPrime;
96
97}
98
99#endif
helper class for primality checks
Definition: prime_helper.h:41
static const unsigned int * firstPrime()
iterator pointing to the first prime in list
Definition: prime_helper.h:73
static bool isPrimeNumber(unsigned int x, bool checkBounds)
checks if a given number is prime
Definition: prime_helper.h:52
static const unsigned int largestNumberForPrimalityCheck
The number up to which this simple primality check is always correct.
Definition: prime_helper.h:44
static const unsigned int * lastPrime()
iterator pointing after the last prime in list
Definition: prime_helper.h:75