Skip to content

Commit 615a69e

Browse files
committed
Fixed sieves
1 parent a3cd552 commit 615a69e

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

math/linear_sieve.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using namespace std;
44

5-
const int maxn = 10101010;
5+
const int maxn = 101010101;
66

77
int lp[maxn];
88

@@ -33,4 +33,5 @@ int main()
3333
for (int p : ans)
3434
cout << p << " ";
3535
cout << "\n";
36+
// cout << ans.size() << "\n";
3637
}

math/sieve.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@ const int maxn = 101010101;
66

77
char s[maxn];
88
// bitset<maxn> s;
9-
vector<int> primes;
109

11-
12-
void init()
10+
vector<int> sieve(int n)
1311
{
14-
for (int i = 2; i*i < maxn; i++)
12+
for (int i = 2; i*i <= n; i++)
1513
if (!s[i])
16-
for (int j = i*i; j < maxn; j += i)
14+
for (int j = i*i; j <= n; j += i)
1715
s[j] = 1;
1816

19-
for (int i = 2; i < maxn; i++)
17+
vector<int> primes;
18+
for (int i = 2; i <= n; i++)
2019
if (!s[i]) primes.push_back(i);
20+
21+
return primes;
2122
}
2223

2324
int main()
2425
{
25-
init();
26+
int n;
27+
cin >> n;
28+
29+
auto ans = sieve(n);
30+
31+
cout << ans.size() << "\n";
2632
}

0 commit comments

Comments
 (0)