Decomposition into primes

Asked

Viewed 623 times

2

Hello folks I’m learning C++ for Data Structure subject and there is some error in the code that is creating an infinite loop.

#include <stdio.h>

int fatores(int a[], int n, int *x) {
    int primo = 2;    // primeiro primo
    int qtdzero = 0;  // incrementa sempre que o resto do número der zero.
    int divisoes = n; // só recebe do parâmetro
    int count = 0;    // contador pra pular o endereço do vetor a

    while (divisoes > 1) {   // vai sair do loop quando divisão chegar a 1
     bool sair = true; 
     for (int i = 1 ; i <= primo ; i++) {
         if (primo % i == 0) {     //teste se é primo ou não
             qtdzero++;
         }
     }
     if (qtdzero == 2) {   //se for primo entra aqui.
         while (sair) {
            if ((divisoes % primo == 0) && (divisoes >= primo)) {
                a[count++] = primo;   //armazena o primo
                divisoes = divisoes / primo;
                (*x)++;
            } else {
                sair = false;
                primo++;
            }
         }
     } else { // se falso, passa pro próximo número e faz o mesmo teste
         primo++;
         qtdzero = 0;
     }
    }
    return ((*x > 10) ? 1 : 0); // retorna quantidade de decomposições
}

int main() {
  int *a = new int[20]; //guarda os números primos
  int *x = new int; // guarda a quantidade de vezes que foi decomposto
  int res = fatores(a, 24, x);

  printf("%i\n", res); // printa o retorno da função fatores

  for (int i = 0; i < 20 ; i++) // exibe os valores decompostos
    printf("%i\n", a[i]);
}
  • If you give me time I’ll take a look but the code is very confusing, it’s really easy to get lost in there. I’m sure if it was written otherwise the mistake wouldn’t happen.

1 answer

1


The code really is a bit confusing. It’s going into infinite loop because divisoes starts at 24, then becomes 12, then 6 and finally 3. When divisoes has 3 does not enter this condition:

if ((divisoes % primo == 0) && (divisoes >= primo))

This occurs the first time when primo has 2 and the rest of the division is not zero. The code falls on this else more internal:

else 
{
     sair = false;
     primo++;
}

Here it did not Zera the variable qtdzero that defines whether the number is prime or not so it will not take the test when primo value 3. He will only return to the test when cousin 5, because when 3 entered the second else and zeroed qtdzero. To not change much your way of building the code do so:

if ((divisoes % primo == 0) && (divisoes >= primo)) 
{
     a[count++] = primo;
     divisoes = divisoes / primo;
     (*x)++;
} 
else 
{
     sair = false;
     primo++;
     qtdzero = 0;
}
  • Thank you for answering but I’m still in doubt, in case when you get to 3 he would 3 % 2 == 0 what gives false, then he goes to He who increases the cousin, going back to the first while doing the same procedure ? In case, prime would be 3 and enter if, divisions would go to 1 and leave the first while.

  • It worked, it was just that qtdzero I forgot to zero. Thanks Note: I saw other examples of decomposition much simpler, I really have to practice more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.