码迷,mamicode.com
首页 > 其他好文 > 详细

查找素数

时间:2016-08-01 01:41:16      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

 

 

位工具

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%左右的时间。

    如图:技术分享

 

查找素数

标签:

原文地址:http://www.cnblogs.com/datakv/p/5724414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!