位工具
void setbit(int pos, unsigned char * flags) { unsigned char on = 1 << (pos % 8); flags[pos / 8] |= on; } void unsetbit(int pos, unsigned char * flags) { unsigned char on = ~(1 << (pos % 8)); flags[pos / 8] &= on; } bool getbit(int pos, unsigned char * flags) { if((flags[pos / 8] & (1 << (pos % 8))) > 0) return 1; return 0; }
void FindPrimeNums(int n) { int flags_occupy = (n + 1) / 8 + 1; unsigned char * flags = new unsigned char[flags_occupy]; memset(flags, 0xFF, flags_occupy); int laste = sqrt(n); for (int i = 2; i <= n; ++ i) if (getbit(i, flags)) { cout << i << "\t"; if(i <= laste) for (int j = 2; j * i <= n; ++ j) unsetbit(i * j, flags); } cout << endl; delete[] flags; }
去掉平方判断
void FindPrimeNums2(int n) { int flags_occupy = (n + 1) / 8 + 1; unsigned char * flags = new unsigned char[flags_occupy]; memset(flags, 0xFF, flags_occupy); for (int i = 2; i <= n; ++ i) if (getbit(i, flags)) { cout << i << "\t"; for (int j = 2; j * i <= n; ++ j) unsetbit(i * j, flags); } cout << endl; delete[] flags; }
测试
#include <iostream> #include <vector> #include<ctime> using namespace std; class CTimer { public: CTimer() { _start=clock(); } ~CTimer() { _end=clock(); cout<< float(_end - _start) / CLK_TCK <<endl; } private: clock_t _start; clock_t _end; }; int main() { CTimer * pct = new CTimer; FindPrimeNums(20000000); delete pct; pct = new CTimer; FindPrimeNums2(20000000); delete pct; return 0; }
测试结果:
去除打印时间,查找20000000以内的素数,排除平方根以上的候选数可以调高20%左右的时间。
如图:
时间: 2024-10-10 11:46:10