C - Print the N-prime prime number

0

The exercise asks the program to read an N number and print out the N-numbered prime number. Ex: I enter with N = 3. The program will have to print The third prime number, that is 5. Other examples: N-esimo (2) = 3, N-esimo (5) = 11. I did the following: p>

void nesimo_primo(int n)
{
    int a, primo, div, nesimo;
    primo = 0;
    a = 1;
    if(n == 1)
        nesimo = 2;
    else
    {
        for(nesimo = 3; nesimo <= 1000; nesimo++)
        {
            for(div = 2; div <= nesimo/2; div++)
            {
                if(nesimo%div != 0)
                    primo++;
                if(primo == 1)
                    a = a + 1;
            }
            if(a == n)
                break;
        }
    }
    printf("N-esimo(%d) = %d\n", a, nesimo);
}

I wonder if logic makes sense and what could be wrong for the program to not be working properly.

    
asked by anonymous 01.05.2017 / 18:02

2 answers

0
void nesimo_primo(int n){
int a, primo, div, nesimo;

primo = 1; //primo se inicia com 1

a = 1;

if(n == 1)
    nesimo = 2;
else
{
    for(nesimo = 3; nesimo <= 1000; nesimo++)
    {
        for(div = 2; div <= nesimo/2; div++)
        {
            if(nesimo%div == 0) // se em algum momento durante o loop, a divisao der exata, sinalize q o numero nao é primo, e saia do loop
            {
                primo = 0;
                break;
            }
        }

        if(primo == 1) //se depois de passar pelo loop a flag ainda estiver ativa, faca
            a++;
        else
            primo = 1;

        if(a == n) 
            break;
    }
}
printf("N-esimo(%d) = %d\n", a, nesimo);}

The premise was correct, if you put a print in your "if (a == n)" will see that it never entered there, if you send the prince within the second for, you will see that it increases to values mto high, because you never reset the value of it.

The problem is that your cousin only calculates to the number 1000, below a different solution.

int PrimoOuNao(unsigned n){
    register int i;

    if( n == 2 )
        return 1;

    if( !(n%2) || (n == 1) )
        return 0;


    for(i = 3; i <= ceil(sqrt((double)n)); i += 2)
        if( !(n%i) )
            return 0;

    return 1;
}

unsigned Nesimo(int n){
register int i;

    if( n == 1 )
        return 2u;

    int contador = 1, primoAtual = 3;

    while( contador < n )
        if( PrimoOuNao(primoAtual++) )
            contador++;

    return --primoAtual;
}
    
01.05.2017 / 19:50
0

First of all it is to clear the values of the verification variables, and knowing that the prime number is only divided by 1 and by itself, must stipulate the limit of divisions that it can do. >

To get the nth prime number, you need to check how many prime numbers were found, so it's good to use a counter.

Let's start the prime number in 1 for hit results, n3 = 5 , n5 = 11 . count is the counter that indicates how many prime numbers were found; rest is the sum of the rest of the integer divisions found; x is the variable to go through for ;

int enesimo_primo(int n){
    int primo = 1;
    int count = 0;
    int rest;
    int x;

The counter must always remain the same until it finds a prime number, so a while must be used for the verification;

    while(count < n){

The increment of the prime number must be happening to each Loop of while , and you should always be cleaning the check variables for a new check.

        // incremento do número primo
        primo += (primo<3) ? 1 : 2;
        rest = 0;

To check the number if it is / not prime, use for , because we know the number limit.

        for(x=1; x<=primo/2; x++){
            if(primo%x == 0) // acrescenta divisão exata
                rest+=1;
            if(rest > 1) // encerra caso o número não seja primo
                continue;
        }
        if(rest == 1) // se rest diferente de 1, o número não é primo
            count ++;
    }

Finally, you can display the value on the screen or return it.

    return primo;
}

main:

int main(int argc, const char **argv){
    int x = nesimo_primo(3);
    printf("%d\n", x);
    x = nesimo_primo(4);
    printf("%d\n", x);
    x = nesimo_primo(5);
    printf("%d\n", x);
    x = nesimo_primo(6);
    printf("%d\n", x);
}

saída:

5
7
11
13
    
01.05.2017 / 19:38