Which is the fastest algorithm to find prime numbers?

后端 未结 14 1678
情深已故
情深已故 2020-11-22 06:49

Which is the fastest algorithm to find out prime numbers using C++? I have used sieve\'s algorithm but I still want it to be faster!

14条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 07:12

    #include 
    
    using namespace std;
    
    int set [1000000];
    
    int main (){
    
        for (int i=0; i<1000000; i++){
            set [i] = 0;
        }
        int set_size= 1000;
        set [set_size];
        set [0] = 2;
        set [1] = 3;
        int Ps = 0;
        int last = 2;
    
        cout << 2 << " " << 3 << " ";
    
        for (int n=1; n<10000; n++){
            int t = 0;
            Ps = (n%2)+1+(3*n);
            for (int i=0; i==i; i++){
                if (set [i] == 0) break;
                if (Ps%set[i]==0){
                    t=1;
                    break;
                }
            }
            if (t==0){
                cout << Ps << " ";
                set [last] = Ps;
                last++;
            }
        }
        //cout << last << endl;
    
    
        cout << endl;
    
        system ("pause");
        return 0;
    }
    

提交回复
热议问题