Strong and weak primes: Difference between revisions

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


int main() {
int main() {
Line 318: Line 318:


// find the prime numbers up to array_size
// find the prime numbers up to array_size
sieve_of_eratosthenes sieve(array_size);
prime_sieve sieve(array_size);


// write numbers with groups of digits separated according to the system default locale
// write numbers with groups of digits separated according to the system default locale
Line 356: Line 356:
}</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 367: Line 367:
* 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 380: Line 380:
* @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 400: Line 400:
* @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;