Successive prime differences: Difference between revisions

Content added Content deleted
m (Minor edit to C++ code)
m (C++ - renamed class)
Line 144: Line 144:
#include <cstdint>
#include <cstdint>
#include <vector>
#include <vector>
#include "sieve_of_eratosthenes.h"
#include "prime_sieve.hpp"


using integer = uint32_t;
using integer = uint32_t;
Line 202: Line 202:
const integer limit = 1000000;
const integer limit = 1000000;
const size_t max_group_size = 4;
const size_t max_group_size = 4;
sieve_of_eratosthenes sieve(limit);
prime_sieve sieve(limit);
diffs d[] = { {2}, {1}, {2, 2}, {2, 4}, {4, 2}, {6, 4, 2} };
diffs d[] = { {2}, {1}, {2, 2}, {2, 4}, {4, 2}, {6, 4, 2} };
vector group;
vector group;
Line 221: Line 221:
}</lang>
}</lang>


Contents of sieve_of_eratosthenes.h:
Contents of prime_sieve.hpp:
<lang cpp>#ifndef SIEVE_OF_ERATOSTHENES_H
<lang cpp>#ifndef PRIME_SIEVE_HPP
#define SIEVE_OF_ERATOSTHENES_H
#define PRIME_SIEVE_HPP


#include <algorithm>
#include <algorithm>
Line 232: Line 232:
* See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.
* See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.
*/
*/
class sieve_of_eratosthenes {
class prime_sieve {
public:
public:
explicit sieve_of_eratosthenes(size_t);
explicit prime_sieve(size_t);
bool is_prime(size_t) const;
bool is_prime(size_t) const;
private:
private:
Line 245: Line 245:
* @param limit the maximum integer that can be tested for primality
* @param limit the maximum integer that can be tested for primality
*/
*/
inline sieve_of_eratosthenes::sieve_of_eratosthenes(size_t limit) {
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 265: Line 265:
* @return true if the integer is prime
* @return true if the integer is prime
*/
*/
inline bool sieve_of_eratosthenes::is_prime(size_t n) const {
inline bool prime_sieve::is_prime(size_t n) const {
if (n == 2)
if (n == 2)
return true;
return true;