prime numbers between a sequence

Asked

Viewed 117 times

0

The problem is this, I have to receive an integer value and calculate the factorial of this number (until then quiet), and take and print on the screen the prime numbers that exist between one and the factorial, but I’m having problems to do this, someone can tell me what is wrong?

/*Mostrar todos os numeros primos*/
# include<stdio.h>
# include<stdlib.h>

int main()
{
  long int numero,fatorial=1;
  int cont,contador=1,teste,truvs,bash=0,primo;

  scanf("%ld",&numero);

  for(cont=1;cont<=numero;cont++)
  {
    fatorial*=cont;
  }

  printf("FATORIAL -> %ld\n",fatorial);//só printei pra testar a fatoração
//---------DELIMITA A OPERAÇAO---------
  for(contador=2;contador<=fatorial;contador++)
  {
    bash=0;

//--------TESTA  PRIMO------------
    for(teste=1;teste<=contador;teste++)
    {
      bash=0;
//---------ISOLAR O TESTE DE DIVISAO-----------
      while(bash<2)
      {

        // truvs=contador%teste;
        if(contador%teste==0)
        {
          bash++;
           printf("%d\n",bash);
        }
        else
        {
          break;
        }


      }

      if(bash==2)
      {
        printf("%d\n",contador);
      }
      // bash=0;

    }
  }



return 0;
} //Fim do programa

1 answer

1

Your test to check if cousin is wrong. Try:

for (teste=1;teste<=contador;teste++)
{
  bash=0;
  div = 1;
  while(div <= teste)
  {
    if (teste%div == 0)
    {
       bash++;
    }
    div++;
  }
  if (bash==2)
  {
    printf("%d\n", teste); /* teste é primo */
  }
}

Note: can be optimized.

Browser other questions tagged

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