Sequence of primorial primes: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
m (Put Sieve of Eratosthenes implementation in separate header file) |
||
Line 98: | Line 98: | ||
#include <iostream> |
#include <iostream> |
||
#include <sstream> |
#include <sstream> |
||
⚫ | |||
#include <gmpxx.h> |
#include <gmpxx.h> |
||
#include "sieve_of_eratosthenes.h" |
|||
typedef mpz_class integer; |
typedef mpz_class integer; |
||
void find_primes(std::vector<bool>& isprime, size_t limit) |
|||
⚫ | |||
isprime.assign(limit, true); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
bool is_prime(const integer& n) |
bool is_prime(const integer& n) |
||
Line 127: | Line 113: | ||
const size_t max = 20; |
const size_t max = 20; |
||
sieve_of_eratosthenes sieve(max_prime); |
|||
⚫ | |||
find_primes(isprime, max_prime); |
|||
integer primorial = 1; |
integer primorial = 1; |
||
for (size_t p = 0, count = 0, index = 0; p < max_prime && count < max; ++p) |
for (size_t p = 0, count = 0, index = 0; p < max_prime && count < max; ++p) |
||
{ |
{ |
||
if (! |
if (!sieve.is_prime(p)) |
||
continue; |
continue; |
||
primorial *= p; |
primorial *= p; |
||
Line 149: | Line 134: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
Contents of sieve_of_eratosthenes.h: |
|||
<lang cpp>#ifndef SIEVE_OF_ERATOSTHENES_H |
|||
#define SIEVE_OF_ERATOSTHENES_H |
|||
⚫ | |||
class sieve_of_eratosthenes |
|||
⚫ | |||
public: |
|||
explicit sieve_of_eratosthenes(size_t); |
|||
bool is_prime(size_t) const; |
|||
private: |
|||
⚫ | |||
}; |
|||
inline bool sieve_of_eratosthenes::is_prime(size_t n) const |
|||
{ |
|||
return is_prime_[n]; |
|||
⚫ | |||
inline sieve_of_eratosthenes::sieve_of_eratosthenes(size_t max) |
|||
: is_prime_(max, true) |
|||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
#endif</lang> |
|||
{{out}} |
{{out}} |