permlib
0.2.6
Library for permutation computations
|
00001 // --------------------------------------------------------------------------- 00002 // 00003 // This file is part of PermLib. 00004 // 00005 // Copyright (c) 2009-2011 Thomas Rehn <thomas@carmen76.de> 00006 // All rights reserved. 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions 00010 // are met: 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 3. The name of the author may not be used to endorse or promote products 00017 // derived from this software without specific prior written permission. 00018 // 00019 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00020 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00021 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00022 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00023 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00024 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00028 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // --------------------------------------------------------------------------- 00031 00032 #ifndef PRIME_HELPER_H_ 00033 #define PRIME_HELPER_H_ 00034 00035 #include <algorithm> 00036 #include <boost/assert.hpp> 00037 00038 namespace permlib { 00039 00041 class PrimeHelper { 00042 public: 00044 static const unsigned int largestNumberForPrimalityCheck; 00045 00047 00052 static bool isPrimeNumber(unsigned int x, bool checkBounds) { 00053 if (checkBounds && x > largestNumberForPrimalityCheck) { 00054 // number too big for our simple check 00055 BOOST_ASSERT( false ); 00056 return false; 00057 } 00058 00059 if (x > largestNumberForPrimalityCheck) { 00060 // the number is to big for our simple check 00061 return false; 00062 } else if (x > largestPrime) { 00063 for (unsigned int i = 0; i < numberOfPrimes; ++i) { 00064 if ((x % primes[i]) == 0) 00065 return false; 00066 } 00067 return true; 00068 } 00069 return std::binary_search(primes, primes+numberOfPrimes, x); 00070 } 00071 00073 static const unsigned int* firstPrime() { return primes; } 00075 static const unsigned int* lastPrime() { return primes + numberOfPrimes; } 00076 private: 00077 static const unsigned int numberOfPrimes; 00078 // This list is probably incomplete ;) 00079 static const unsigned int primes[]; 00080 static const unsigned int largestPrime; 00081 }; 00082 00083 const unsigned int PrimeHelper::numberOfPrimes = 170; 00084 // Probably this list is incomplete ;) 00085 const unsigned int PrimeHelper::primes[numberOfPrimes] = { 00086 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, 00087 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, 00088 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, 00089 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, 00090 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, 00091 877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013 00092 }; 00093 const unsigned int PrimeHelper::largestPrime = primes[numberOfPrimes-1]; 00094 const unsigned int PrimeHelper::largestNumberForPrimalityCheck = largestPrime * largestPrime; 00095 00096 } 00097 00098 #endif