Safe primes and unsafe primes: Difference between revisions
m
Put Sieve of Eratosthenes implementation in separate header file
Thundergnat (talk | contribs) m (Fix Perl 6 -> Raku in comments) |
m (Put Sieve of Eratosthenes implementation in separate header file) |
||
Line 281:
#include <locale>
#include <sstream>
#include
int main()
Line 290:
// find the prime numbers up to limit2
isprime[0] = isprime[1] = false;▼
for (int p = 2; p * p < limit2; ++p)▼
{▼
if (isprime[p])▼
{▼
for (int i = p * p; i < limit2; i += p)▼
isprime[i] = false;▼
}▼
}▼
// write numbers with groups of digits separated according to the system default locale
Line 312 ⟶ 303:
for (int p = 2; p < limit2; ++p)
{
if (!
continue;
int safe =
++count2[safe];
if (p < limit1)
Line 333 ⟶ 324:
return 0;
}</lang>
Contents of sieve_of_eratosthenes.h:
<lang cpp>#ifndef SIEVE_OF_ERATOSTHENES_H
#define SIEVE_OF_ERATOSTHENES_H
#include <vector>
class sieve_of_eratosthenes
{
public:
explicit sieve_of_eratosthenes(size_t);
bool is_prime(size_t) const;
private:
std::vector<bool> is_prime_;
};
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}}
|