zoukankan      html  css  js  c++  java
  • 查找素数

    位工具

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

        如图:

    出自datakv
  • 相关阅读:
    CFree 提示main must return int
    策略模式
    CFree 提示no newline at the end of file
    EEPROM的写入操作解析
    一些关于mic的知识
    一些关于电池的资料
    太阳能电池板日发电量简易计算方法
    ubuntu 下载编译android源代码
    SC44B0的内存地址解析
    RequireJS 2.0 学习笔记一
  • 原文地址:https://www.cnblogs.com/datakv/p/5724414.html
Copyright © 2011-2022 走看看