Infinite loop trying to calculate if the number is prime

Asked

Viewed 1,608 times

6

I have no idea how I’m going to determine the condition of for for prime numbers. I always end in looping.

#include <stdio.h>

int main() {

    int num = 1, primo;
    do {

        printf("Informe um numero primo maior que 1:");
        scanf("%d", &primo);

    } while (primo <= 1);



    for (num=1; num<=primo; primo++){

       if (primo%num==0)
           printf("Numero primo",primo);
       else
           printf("Numero não primo",primo);
    }

}
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

10

To program you have to think of everything as the problem really is. It’s no use kicking things. This is not programming. One of the things that helps clarify the thoughts is to keep the code well organized, easy to read and understand, including giving names to variables that are significant, indicating what that name actually represents. You have to think the right name for everything, otherwise it creates confusion as seen in this code.

You can only know if a number is prime or not after you have tested all possibilities, you can’t know by testing each of the possibilities, so the check can’t be inside the loop.

#include <stdio.h>

int main() {
    int i, numero;
    do {
        printf("Informe um numero primo maior que 1:");
        scanf("%d", &numero);
    } while (numero <= 1);
    for (i = 2; i < numero; i++) {
        if (numero % i == 0) {
            break;
        }
    }
    if (numero == i) {
        printf("Numero primo %d", numero);
    } else {
        printf("Numero não primo %d", numero);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • No, I believe it is i < numero, nay?

  • 1

    @Felipeavelar yes, of course, thank you.

  • Or you could optimize the code for i*i <= numero, since you only need to go up numero^(1/2) to know if he is cousin or not. (:

  • 1

    I did not want to complicate for him that is starting in this case. I already have an answer that I use this technique. http://answall.com/a/50500/101

0

To check whether a number is prime or not, you don’t need to do all the checks from 2 to n. Because the number 2 is the only even prime number, so to check whether 11 is prime or not, you only need to check up to 5, because the remaining numbers are multiples of 1, 2, 3, 4 and 5, that is, 3x2=6, 4x2=8 and 5x2=10, since all natural numbers multiplied by 2 are even, so they will not be prime, except for 2.So, if you are going to check whether a very large number is prime or not, you will get the result faster.

int isPrimeNumber(int num){
    if(num <= 1){
        return 0; //Retorna falso
    } else {
        int quant = num / 2;
        for(; quant >= 2; --quant)
            if(num % quant == 0)
                return 0; //Retorna falso
        return 1; //Retorna verdadeiro
    }
}

Code on the ideone: Prime number

Browser other questions tagged

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