Prime Number Algorithm

前端 未结 7 1954
梦如初夏
梦如初夏 2020-12-19 05:14

Can anyone tell me how to implement Sieve of Eratosthenes algorithm in C? I need to generate prime numbers but my algorithm is slow.

My code:

#inclu         


        
7条回答
  •  抹茶落季
    2020-12-19 06:15

    Though it's very old post, Following is my try to generate the prime number using "Sieve of Eratosthenes" algorithm.

    #include 
    
    #define NUM 8000        /* Prime Numbers in the Range.  'Range + 2' actually. */
    
    int main()
    {
      int a[NUM] = {0};         /* Array which is going to hold all the numbers */
      int i , j;
    
      /* initializing array from 2 to given number + 2, assuming the first prime number is 2 */
      for(i = 2,j=0; i < NUM+2, j

    Hope this will help someone.

提交回复
热议问题