Advertisement
imcm

Quase-Primo [TH 1014]

Apr 24th, 2017
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. /*
  2.   Izabella Maria Cavalcanti Melo;
  3.   1º periodo de Engenharia da Computação - CIn UFPE;
  4.   imcm@cin.ufpe.br;
  5.   21/04/2017
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11.  
  12. int primo(x) {
  13.     int a=1, b, div=0;
  14.      while(a<=x) {
  15.       b = x%a;                        //se x mod a for igual a zero, a é divisor de x
  16.       if (b==0) {
  17.         div = div+1;
  18.         a = a + 1; }
  19.       else a = a + 1; }
  20.      return div; }
  21.  
  22. int main()
  23. {
  24.     int t, n, i, j=0, k=0;
  25.     scanf("%d", &t);
  26.  
  27.     for (i=t; i>0; i--) {
  28.       scanf("%d", &n);
  29.  
  30.        if (n==1) printf("Nem primo nem quase primo\n");
  31.  
  32.        else if (primo(n)==2) printf("Primo\n");
  33.  
  34.        else if (primo(n)!=2) {
  35.  
  36.     /* para todo quase primo n par, em n/p, p é necessariamente 2, pois se p for multiplo de 2, não será primo;
  37.     e se p não for 2 nem um multiplo de 2, não será par */
  38.  
  39.        if (n%2==0) {
  40.         if ((primo(n/2)==2)&&(2!=n/2)) printf("Quase primo\n");
  41.         else printf("Nem primo nem quase primo\n"); }
  42.  
  43.        else if (n%2!=0) {
  44.         while (j<=n) {
  45.            j=j+1;
  46.            if ((n%j==0)&&(primo(j)==2)) {
  47.                 k=n/j;
  48.  
  49.            if ((primo(k)==2)&&(k!=j)) {
  50.                 printf("Quase primo\n");
  51.                 break; } }
  52.  
  53.            else if (j==n+1)
  54.             printf("Nem primo nem quase primo\n");
  55.         }
  56.        }
  57.     }
  58.     j=0;
  59.     k=0;
  60.     }
  61.     return 0;
  62. }
  63. //imcm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement