File tree 2 files changed +32
-4
lines changed
2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change 1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
#include <stdbool.h>
4
- #include <string.h>
5
4
6
- static int countPrimes (int n )
5
+
6
+ int countPrimes (int n )
7
7
{
8
- if (n < 3 ) return 0 ;
9
-
8
+ if (n < 3 ) {
9
+ return 0 ;
10
+ }
11
+
10
12
int i , j ;
11
13
bool * marked = malloc (n );
12
14
memset (marked , false, n );
Original file line number Diff line number Diff line change
1
+ #include < stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ class Solution {
6
+ public:
7
+ int countPrimes (int n) {
8
+ if (n < 3 ) {
9
+ return 0 ;
10
+ }
11
+
12
+ vector<bool > marked (n);
13
+ int count = n >> 1 ;
14
+ for (int i = 3 ; i * i <= n; i += 2 ) {
15
+ if (!marked[i]) {
16
+ for (int j = i * i; j < n; j += (i << 1 )) {
17
+ if (!marked[j]) {
18
+ marked[j] = true ;
19
+ --count;
20
+ }
21
+ }
22
+ }
23
+ }
24
+ return count;
25
+ }
26
+ };
You can’t perform that action at this time.
0 commit comments