Guest User

Untitled

a guest
Jan 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. //Golbach Conjecture
  2. //Sem Loops (No Loops)
  3.  
  4. #include<stdio.h>
  5. #define TAM 100000
  6.  
  7. int  primos[TAM], primo[TAM], TAMANHO = 0, LIMITE, pmaior, pmin, soma, numero;
  8. void preenche_vetor(int);
  9. void zerar_primos(int);
  10. void jota(int, int);
  11. void primo_f(int, int);
  12. void limite(int, int);
  13. void Goldbach(int, int, int);
  14. void listaGoldbach(int);
  15.  
  16. void preenche_vetor(int x)
  17. {
  18.    if(x != TAM)
  19.    {
  20.       primos[x] = x + 1;
  21.       preenche_vetor(x + 1);
  22.    }
  23. }
  24.  
  25. void zerar_primos(int x)
  26. {
  27.    int j;
  28.    if(x != TAM)
  29.    {
  30.       if(primos[x] != 0)
  31.       {
  32.      j = x + primos[x];
  33.      if(j*2 < TAM)
  34.      {
  35.         jota(j, x);
  36.      }
  37.      else
  38.             return;
  39.       }
  40.       zerar_primos(x + 1);
  41.    }
  42. }
  43.  
  44. void jota(int j, int x)
  45. {
  46.    if(j < TAM)
  47.    {
  48.       primos[j] = 0;
  49.       jota(j + primos[x], x);
  50.    }
  51. }
  52.  
  53. void primo_f(int x, int y)
  54. {
  55.    if(x != TAM)
  56.    {
  57.       if(primos[x] != 0)
  58.       {
  59.      primo[y] = primos[x];
  60.      TAMANHO++;
  61.      primo_f(x + 1, y + 1);
  62.       }
  63.       else
  64.      primo_f(x + 1, y);
  65.    }
  66. }
  67.  
  68. void limite(int x, int y)
  69. {
  70.    if(primo[y] <= x)
  71.       limite(x, y + 1);
  72.    else
  73.       LIMITE = y;
  74. }
  75.  
  76. void Goldbach(int num, int limit, int min)
  77. {
  78.     pmaior = primo[limit];
  79.     pmin = primo[min];
  80.     soma = pmaior + pmin;
  81.     if(soma < num)
  82.        Goldbach(num, limit, min + 1);
  83.     else if(soma > num)
  84.        Goldbach(num, limit - 1, 0);
  85. }
  86.  
  87. void listaGoldbach(int x)
  88. {
  89.    if(x <= numero)
  90.    {
  91.       limite(x, 0);
  92.       Goldbach(x, LIMITE - 1, 0);
  93.       printf("%d = %d + %d\n", x, pmaior, pmin);
  94.       listaGoldbach(x + 2);
  95.    }
  96.  
  97. }
  98.  
  99. int main()
  100. {
  101.    printf("Digite um número: ");
  102.    scanf("%d", &numero);
  103.    if(numero % 2 != 0)
  104.       printf("\nO número deve ser par!\n");
  105.    else
  106.    {
  107.       preenche_vetor(0);
  108.       primos[0] = 0;
  109.       zerar_primos(1);
  110.       primo_f(0, 0);
  111.       listaGoldbach(4);
  112.    }
  113.    return 0;
  114. }
Add Comment
Please, Sign In to add comment