-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcountPrimes.cpp
More file actions
58 lines (49 loc) · 1.17 KB
/
countPrimes.cpp
File metadata and controls
58 lines (49 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// https://leetcode.com/problems/count-primes/
//
// Status: Accepted
// Runtime: 16 ms
// Score: 97.57 %
//
#include <iostream>
#include <vector>
#include <gtest/gtest.h>
#include "args.h"
#include "macros.h"
using namespace std;
// class definition comes from leetcode
class Solution
{
public:
int countPrimes(int n)
{
int numPrimes = 0;
// By definition, 1 is not a prime
if (n == 1)
return 0;
// Making this bigger to not deal with 0-indexing issue
bool *notPrimes = new bool[n + 1]();
for (int i = 2; i < n; i++)
{
// Don't bother if this isn't a prime
if (notPrimes[i])
{
continue;
}
numPrimes++;
notPrimes[i] = true;
// Mark off all multiples as not a prime
for (int j = 1; (j * i) < n; j++)
{
notPrimes[j * i] = true;
}
}
return numPrimes;
};
};
TEST(CountPrimes, Examples)
{
Solution solution;
ASSERT_EQ(solution.countPrimes(10), 4);
ASSERT_EQ(solution.countPrimes(0), 0);
ASSERT_EQ(solution.countPrimes(1), 0);
}