Prime Number Algorithm

前端 未结 7 1947
梦如初夏
梦如初夏 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:16

    Here is actually very simple code that use the Sieve of Eratosthenes algorithm. works for all positive int.

    int is_prime(int n){
      int p;
      for(p = 2; p < n; p++){
        if(n % p ==0 && p != n)
          return 0;    
      }
      return 1;
    }
    
    0 讨论(0)
提交回复
热议问题