Advertisement
luizaspan

Prime check II

May 12th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.52 KB | None | 0 0
  1. // achar os primeiros n numeros primos
  2. // o número primo n tem 4 divisores inteiros: n, 1, -1, -n
  3.  
  4. #include <stdio.h>
  5.  
  6. int main(void)
  7. {
  8.  
  9.   int x,n,i,k;
  10.  
  11.  
  12.   for (n=1;n<=10000;n++)
  13.     {
  14.       k=0; // tendeu?
  15.       for (i=-n;i<=n;i++)
  16.     {
  17.       if (i==0)
  18.         i++;
  19.  
  20.       x=n%i;
  21.  
  22.       if (x==0)
  23.         {        
  24.           k++;
  25.         }    
  26.     }
  27.  
  28.       if (k==4)
  29.     {
  30.       printf("O número %d é primo.\n",n);
  31.     }
  32.  
  33.       else if (k!=4)
  34.     {
  35.       printf("O número %d não é primo.\n",n);
  36.     }
  37.     }
  38.      
  39.  
  40.   return 0;
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement