Which is the fastest algorithm to find prime numbers?

后端 未结 14 1583
情深已故
情深已故 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 06:58

    I always use this method for calculating primes numbers following with the sieve algorithm.

    void primelist()
     {
       for(int i = 4; i < pr; i += 2) mark[ i ] = false;
       for(int i = 3; i < pr; i += 2) mark[ i ] = true; mark[ 2 ] = true;
       for(int i = 3, sq = sqrt( pr ); i < sq; i += 2)
           if(mark[ i ])
              for(int j = i << 1; j < pr; j += i) mark[ j ] = false;
      prime[ 0 ] = 2; ind = 1;
      for(int i = 3; i < pr; i += 2)
        if(mark[ i ]) ind++; printf("%d\n", ind);
     }
    

提交回复
热议问题