37 #ifndef GTEST_SAMPLES_PRIME_TABLES_H_ 38 #define GTEST_SAMPLES_PRIME_TABLES_H_ 48 virtual bool IsPrime(
int n)
const = 0;
59 if (n <= 1)
return false;
61 for (
int i = 2; i*i <= n; i++) {
63 if ((n % i) == 0)
return false;
70 for (
int n = p + 1; n > 0; n++) {
84 : is_prime_size_(max + 1), is_prime_(new bool[max + 1]) {
85 CalculatePrimesUpTo(max);
90 return 0 <= n && n < is_prime_size_ && is_prime_[n];
94 for (
int n = p + 1; n < is_prime_size_; n++) {
95 if (is_prime_[n])
return n;
102 void CalculatePrimesUpTo(
int max) {
103 ::std::fill(is_prime_, is_prime_ + is_prime_size_,
true);
104 is_prime_[0] = is_prime_[1] =
false;
106 for (
int i = 2; i <= max; i++) {
107 if (!is_prime_[i])
continue;
110 for (
int j = 2*i; j <= max; j += i) {
111 is_prime_[j] =
false;
116 const int is_prime_size_;
117 bool*
const is_prime_;
123 #endif // GTEST_SAMPLES_PRIME_TABLES_H_ virtual ~PrimeTable()
Definition: prime_tables.h:45
virtual bool IsPrime(int n) const
Definition: prime_tables.h:58
Definition: prime_tables.h:80
virtual int GetNextPrime(int p) const
Definition: prime_tables.h:93
virtual bool IsPrime(int n) const
Definition: prime_tables.h:89
virtual int GetNextPrime(int p) const
Definition: prime_tables.h:69
Definition: prime_tables.h:56
Definition: prime_tables.h:43
virtual bool IsPrime(int n) const =0
PreCalculatedPrimeTable(int max)
Definition: prime_tables.h:83
virtual ~PreCalculatedPrimeTable()
Definition: prime_tables.h:87
virtual int GetNextPrime(int p) const =0