title: Prime Number Algorithm
body: <p>Can anyone tell me how to implement <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">Sieve of Eratosthenes</a> algorithm in C? I need to generate prime numbers but my algorithm is slow.</p>
  
  <p>My code: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int prime(long int i)
  {
      long int j;
      int state = 1;
      for(j=2;j&lt;i;j++)
      {
          if((i%j)==0){state=0;break;}
      }
      return state;
  }
  
  int main()
  {
      int t;
      long int m,n,i;
      scanf("%d", &amp;t);
      while(t--) {
          scanf("%d %d", &amp;m,&amp;n);
          for(i=m;i&lt;=n;i++)
          {
              if(i==1){
                  //do nothing for 1
              } else{
                  if(prime(i))printf("%d\n",i);
              }
          }
      }
      return 0;
  }
  </code></pre>
  
  <p><code>t</code> is the number of test cases m and n is the range between which prime are to be printed.</p>