?? sieve.cpp
字號:
/**
* @version 1.20 1999-07-03
* author Cay Horstmann
*/
#ifndef AVOID_STANDARD_BITSET
#include <bitset>
#else
template<int N>
class bitset
{
public:
bitset() : bits(new char[(N - 1) / 8 + 1]) {}
bool test(int n)
{ return (bits[n >> 3] & (1 << (n & 7))) != 0;
}
void set(int n)
{ bits[n >> 3] |= 1 << (n & 7);
}
void reset(int n)
{ bits[n >> 3] &= ~(1 << (n & 7));
}
private:
char* bits;
};
#endif
#include <iostream>
using namespace std;
int main()
{ const int N = 1000000;
clock_t cstart = clock();
bitset<N + 1> b;
int count = 0;
int i;
for (i = 2; i <= N; i++)
b.set(i);
i = 2;
while (i * i <= N)
{ if (b.test(i))
{ int k = 2 * i;
while (k <= N)
{ b.reset(k);
k += i;
}
}
i++;
}
for (i = 2; i <= N; i++)
{ if (b.test(i))
{
#ifdef PRINT
cout << i << "\n";
#endif
count++;
}
}
clock_t cend = clock();
double millis = 1000.0
* (cend - cstart) / CLOCKS_PER_SEC;
cout << count << " primes\n"
<< millis << " milliseconds\n";
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -