Download code
From LiteratePrograms
Back to Sieve_of_Eratosthenes_(C_Plus_Plus)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
Sieve.cpp
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Sieve_of_Eratosthenes_(C_Plus_Plus)?action=history&offset=20081217213409 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Sieve_of_Eratosthenes_(C_Plus_Plus)?oldid=15640 25 */ 26 27 #include "Sieve.h" 28 29 std::vector<size_t> sieve_of_eratosthenes(size_t max) 30 { 31 Sieve sieve(max + 1 /* +1 to include max itself*/); 32 size_t i; 33 for(i=3; i*i <= max; i += 2) 34 { 35 if (sieve.is_composite(i)) 36 { 37 continue; 38 } 39 40 // We increment by 2*i to skip even multiples of i 41 for(size_t multiple_i=i*i; multiple_i <= max; multiple_i += 2*i) 42 { 43 sieve.set_composite(multiple_i); 44 } 45 } 46 47 48 std::vector<size_t> primes; 49 primes.push_back(2); 50 for(size_t i=3; i <= max; i += 2) 51 { 52 if (!sieve.is_composite(i)) 53 { 54 primes.push_back(i); 55 } 56 } 57 return primes; 58 } 59 60
Sieve.h
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Sieve_of_Eratosthenes_(C_Plus_Plus)?action=history&offset=20081217213409 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Sieve_of_Eratosthenes_(C_Plus_Plus)?oldid=15640 25 */ 26 27 #ifndef _SIEVE_H_ 28 #define _SIEVE_H_ 29 30 #include <vector> 31 #include <cstdlib> // size_t 32 #include <cassert> 33 34 35 class Sieve 36 { 37 private: 38 std::vector<bool> sieve; 39 public: 40 Sieve(size_t size); 41 bool is_composite(size_t k); 42 void set_composite(size_t k); 43 }; 44 45 inline Sieve::Sieve(size_t size) 46 : sieve((size+1)/2) 47 { } 48 49 inline bool Sieve::is_composite(size_t k) 50 { 51 assert(k >= 3 && (k % 2) == 1); 52 return sieve[(k-3)/2]; 53 } 54 55 inline void Sieve::set_composite(size_t k) 56 { 57 assert(k >= 3 && (k % 2) == 1); 58 sieve[(k-3)/2] = true; 59 } 60 61 62 std::vector<size_t> sieve_of_eratosthenes(size_t max); 63 64 #endif // #ifndef _SIEVE_H_ 65
test.cpp
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Sieve_of_Eratosthenes_(C_Plus_Plus)?action=history&offset=20081217213409 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Sieve_of_Eratosthenes_(C_Plus_Plus)?oldid=15640 25 */ 26 27 #include <cstdlib> // For atoi() 28 #include <ctime> // For clock(), clock_t 29 #include <iostream> 30 #include "Sieve.h" 31 32 using std::cout; 33 using std::endl; 34 35 int main(int argc, char* argv[]) 36 { 37 size_t max = (size_t)atoi(argv[1]); 38 int num_times = 1; 39 if (argc > 2) 40 { 41 num_times = atoi(argv[2]); 42 } 43 44 std::vector<size_t> result; 45 clock_t start_time = clock(); 46 for(int i=0; i < num_times; i++) 47 { 48 result = sieve_of_eratosthenes(max); 49 } 50 double time_in_ms = ((double)(clock() - start_time)) / CLOCKS_PER_SEC / num_times * 1000; 51 double time_per_integer_ns = time_in_ms / max * 1000000; 52 53 cout << "Sieved over integers 1 to " << max 54 << " in " << time_in_ms << " ms (" << time_per_integer_ns << " ns per integer)" 55 << endl << endl; 56 57 for(size_t i=0; i < result.size(); i++) 58 { 59 cout << result[i] << endl; 60 } 61 62 return 0; 63 } 64
