Guest User

Untitled

a guest
Jan 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. void verificacaoDePrimos(int* vetor, int* numero)
  6. {
  7.     int i,j,k,n,l;
  8.     int aux;
  9.  
  10.     n=*numero-2;
  11.  
  12.     for(i=0;i<n;i++)
  13.     {
  14.         l=0;
  15.         for(j=1;j<n;j++)
  16.         {
  17.             if((vetor[i+j])%(vetor[i])==0)
  18.             {
  19.                 for(k=i+j;vetor[k]<vetor[k+1];k++)
  20.                 {
  21.                     aux=vetor[k];
  22.                     vetor[k]=vetor[k+1];
  23.                     vetor[k+1]=aux;
  24.  
  25.                 }
  26.                 n--;
  27.                 l++;
  28.  
  29.             }
  30.  
  31.         }
  32.         if(l!=0)
  33.         {
  34.             aux=*numero;
  35.  
  36.             vetor=(int*)realloc(vetor,(n+1)*sizeof(int));
  37.             if(vetor==NULL)
  38.             {
  39.                 printf("Fudeu");
  40.             }
  41.  
  42.         }
  43.     }
  44.  
  45.  
  46.     for(i=0;i<=n;i++)
  47.     {
  48.         printf("%d ",vetor[i]);
  49.     }
  50.  
  51. }
  52.  
  53.  
  54. void formaVetor(int* numero)
  55. {
  56.     int i,valor;
  57.     int* vetor;
  58.     valor=*numero;
  59.  
  60.     vetor=(int*)malloc((valor-1)*sizeof(int));
  61.  
  62.     if(vetor==NULL)
  63.     {
  64.         printf("Nao tem espaco disponivel na memoria!");
  65.         exit(1);
  66.     }
  67.     else
  68.     {
  69.         for(i=2;i<=valor;i++)
  70.         {
  71.             vetor[i-2]=i;
  72.         }
  73.  
  74.  
  75.         verificacaoDePrimos(vetor,numero);
  76.  
  77.  
  78.  
  79.     }
  80.     free(vetor);
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. int main()
  88. {
  89.     int numero;
  90.  
  91.     do
  92.     {
  93.         printf("Digite o numero e veja todos os primos ate o numero digitado: ");
  94.         scanf("%d",&numero);
  95.  
  96.     }while(numero<=1);
  97.  
  98.     formaVetor(&numero);
  99.  
  100.  
  101.     return 0;
  102. }
Add Comment
Please, Sign In to add comment