2626// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2727// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29- //
30- // Author: wan@google.com (Zhanyong Wan)
31- // Author: vladl@google.com (Vlad Losev)
29+
30+
3231
3332// This provides interface PrimeTable that determines whether a number is a
3433// prime and determines a next prime number. This interface is used
3534// in Google Test samples demonstrating use of parameterized tests.
3635
37- #ifndef GTEST_SAMPLES_PRIME_TABLES_H_
38- #define GTEST_SAMPLES_PRIME_TABLES_H_
36+ #ifndef GOOGLETEST_SAMPLES_PRIME_TABLES_H_
37+ #define GOOGLETEST_SAMPLES_PRIME_TABLES_H_
3938
4039#include < algorithm>
4140
@@ -44,7 +43,7 @@ class PrimeTable {
4443 public:
4544 virtual ~PrimeTable () {}
4645
47- // Returns true iff n is a prime number.
46+ // Returns true if and only if n is a prime number.
4847 virtual bool IsPrime (int n) const = 0;
4948
5049 // Returns the smallest prime number greater than p; or returns -1
@@ -55,7 +54,7 @@ class PrimeTable {
5554// Implementation #1 calculates the primes on-the-fly.
5655class OnTheFlyPrimeTable : public PrimeTable {
5756 public:
58- virtual bool IsPrime (int n) const {
57+ bool IsPrime (int n) const override {
5958 if (n <= 1 ) return false ;
6059
6160 for (int i = 2 ; i*i <= n; i++) {
@@ -66,12 +65,12 @@ class OnTheFlyPrimeTable : public PrimeTable {
6665 return true ;
6766 }
6867
69- virtual int GetNextPrime (int p) const {
70- for (int n = p + 1 ; n > 0 ; n++) {
68+ int GetNextPrime (int p) const override {
69+ if (p < 0 ) return -1 ;
70+
71+ for (int n = p + 1 ;; n++) {
7172 if (IsPrime (n)) return n;
7273 }
73-
74- return -1 ;
7574 }
7675};
7776
@@ -84,13 +83,13 @@ class PreCalculatedPrimeTable : public PrimeTable {
8483 : is_prime_size_(max + 1 ), is_prime_(new bool [max + 1 ]) {
8584 CalculatePrimesUpTo (max);
8685 }
87- virtual ~PreCalculatedPrimeTable () { delete[] is_prime_; }
86+ ~PreCalculatedPrimeTable () override { delete[] is_prime_; }
8887
89- virtual bool IsPrime (int n) const {
88+ bool IsPrime (int n) const override {
9089 return 0 <= n && n < is_prime_size_ && is_prime_[n];
9190 }
9291
93- virtual int GetNextPrime (int p) const {
92+ int GetNextPrime (int p) const override {
9493 for (int n = p + 1 ; n < is_prime_size_; n++) {
9594 if (is_prime_[n]) return n;
9695 }
@@ -103,11 +102,15 @@ class PreCalculatedPrimeTable : public PrimeTable {
103102 ::std::fill (is_prime_, is_prime_ + is_prime_size_, true );
104103 is_prime_[0 ] = is_prime_[1 ] = false ;
105104
106- for (int i = 2 ; i <= max; i++) {
105+ // Checks every candidate for prime number (we know that 2 is the only even
106+ // prime).
107+ for (int i = 2 ; i*i <= max; i += i%2 +1 ) {
107108 if (!is_prime_[i]) continue ;
108109
109110 // Marks all multiples of i (except i itself) as non-prime.
110- for (int j = 2 *i; j <= max; j += i) {
111+ // We are starting here from i-th multiplier, because all smaller
112+ // complex numbers were already marked.
113+ for (int j = i*i; j <= max; j += i) {
111114 is_prime_[j] = false ;
112115 }
113116 }
@@ -120,4 +123,4 @@ class PreCalculatedPrimeTable : public PrimeTable {
120123 void operator =(const PreCalculatedPrimeTable& rhs);
121124};
122125
123- #endif // GTEST_SAMPLES_PRIME_TABLES_H_
126+ #endif // GOOGLETEST_SAMPLES_PRIME_TABLES_H_
0 commit comments