Sexy primes: Difference between revisions
Content added Content deleted
m (→{{header|Lua}}) |
m (C++ - renamed class) |
||
Line 310: | Line 310: | ||
#include <vector> |
#include <vector> |
||
#include <boost/circular_buffer.hpp> |
#include <boost/circular_buffer.hpp> |
||
#include " |
#include "prime_sieve.hpp" |
||
int main() { |
int main() { |
||
Line 326: | Line 326: | ||
// Use Sieve of Eratosthenes to find prime numbers up to max |
// Use Sieve of Eratosthenes to find prime numbers up to max |
||
prime_sieve sieve(array_size); |
|||
std::array<int, max_group_size> group_count{0}; |
std::array<int, max_group_size> group_count{0}; |
||
Line 377: | Line 377: | ||
}</lang> |
}</lang> |
||
Contents of |
Contents of prime_sieve.hpp: |
||
<lang cpp>#ifndef |
<lang cpp>#ifndef PRIME_SIEVE_HPP |
||
#define |
#define PRIME_SIEVE_HPP |
||
#include <algorithm> |
#include <algorithm> |
||
Line 388: | Line 388: | ||
* See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. |
* See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. |
||
*/ |
*/ |
||
class |
class prime_sieve { |
||
public: |
public: |
||
explicit |
explicit prime_sieve(size_t); |
||
bool is_prime(size_t) const; |
bool is_prime(size_t) const; |
||
private: |
private: |
||
Line 401: | Line 401: | ||
* @param limit the maximum integer that can be tested for primality |
* @param limit the maximum integer that can be tested for primality |
||
*/ |
*/ |
||
inline |
inline prime_sieve::prime_sieve(size_t limit) { |
||
limit = std::max(size_t(3), limit); |
limit = std::max(size_t(3), limit); |
||
is_prime_.resize(limit/2, true); |
is_prime_.resize(limit/2, true); |
||
Line 421: | Line 421: | ||
* @return true if the integer is prime |
* @return true if the integer is prime |
||
*/ |
*/ |
||
inline bool |
inline bool prime_sieve::is_prime(size_t n) const { |
||
if (n == 2) |
if (n == 2) |
||
return true; |
return true; |