w_findprime.cpp

00001 /* -*- mode:C++; c-basic-offset:4 -*-
00002      Shore-MT -- Multi-threaded port of the SHORE storage manager
00003    
00004                        Copyright (c) 2007-2009
00005       Data Intensive Applications and Systems Labaratory (DIAS)
00006                Ecole Polytechnique Federale de Lausanne
00007    
00008                          All Rights Reserved.
00009    
00010    Permission to use, copy, modify and distribute this software and
00011    its documentation is hereby granted, provided that both the
00012    copyright notice and this permission notice appear in all copies of
00013    the software, derivative works or modified versions, and any
00014    portions thereof, and that both notices appear in supporting
00015    documentation.
00016    
00017    This code is distributed in the hope that it will be useful, but
00018    WITHOUT ANY WARRANTY; without even the implied warranty of
00019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
00020    DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
00021    RESULTING FROM THE USE OF THIS SOFTWARE.
00022 */
00023 
00024 // -*- mode:c++; c-basic-offset:4 -*-
00025 /*<std-header orig-src='shore' incl-file-exclusion='W_FINDPRIME_CPP'>
00026 
00027  $Id: w_findprime.cpp,v 1.3 2010/06/23 23:42:57 nhall Exp $
00028 
00029 SHORE -- Scalable Heterogeneous Object REpository
00030 
00031 Copyright (c) 1994-99 Computer Sciences Department, University of
00032                       Wisconsin -- Madison
00033 All Rights Reserved.
00034 
00035 Permission to use, copy, modify and distribute this software and its
00036 documentation is hereby granted, provided that both the copyright
00037 notice and this permission notice appear in all copies of the
00038 software, derivative works or modified versions, and any portions
00039 thereof, and that both notices appear in supporting documentation.
00040 
00041 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00042 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00043 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00044 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00045 
00046 This software was developed with support by the Advanced Research
00047 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00048 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00049 Further funding for this work was provided by DARPA through
00050 Rome Research Laboratory Contract No. F30602-97-2-0247.
00051 
00052 */
00053 
00054 #include "w_findprime.h"
00055 #include <vector>
00056 
00057 // lots of help from Wikipedia here!
00058 w_base_t::int8_t w_findprime(w_base_t::int8_t min) 
00059 {
00060     // the first 25 primes
00061     static char const prime_start[] = {
00062     // skip 2,3,5 because our mod60 test takes care of them for us
00063     /*2, 3, 5,*/ 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
00064     43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
00065     };
00066     // x%60 isn't on this list, x is divisible by 2, 3 or 5. If it
00067     // *is* on the list it still might not be prime
00068     static char const sieve_start[] = {
00069     // same as the start list, but adds 1,49 and removes 3,5
00070     1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59
00071     };
00072 
00073     // use the starts to populate our data structures
00074     std::vector<w_base_t::int8_t> primes(prime_start, prime_start+sizeof(prime_start));
00075     char sieve[60];
00076     memset(sieve, 0, sizeof(sieve));
00077 
00078     for(w_base_t::uint8_t i=0; i < sizeof(sieve_start); i++)
00079     sieve[w_base_t::int8_t(sieve_start[i])] = 1;
00080 
00081     /* We aren't interested in 4000 digit numbers here, so a Sieve of
00082        Erastothenes will work just fine, especially since we're
00083        seeding it with the first 25 primes and avoiding the (many)
00084        numbers that divide by 2,3 or 5.
00085      */
00086     for(w_base_t::int8_t x=primes.back()+1; primes.back() < min; x++) {
00087     if(!sieve[x%60])
00088         continue; // divides by 2, 3 or 5
00089 
00090     bool prime = true;
00091     for(w_base_t::int8_t i=0; prime && primes[i]*primes[i] <= x; i++) 
00092         prime = (x%primes[i]) > 0;
00093 
00094     if(prime) 
00095         primes.push_back(x);
00096     }
00097 
00098     return primes.back();
00099 }

Generated on Thu Dec 9 08:42:27 2010 for Shore Storage Manager by  doxygen 1.4.7