Last active
February 22, 2017 02:47
-
-
Save drmcarvalho/deb36004eb8162dd50265ebed6c2f4b2 to your computer and use it in GitHub Desktop.
Função para saber se um numero é primo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(void) { | |
printf("0 é primo %s\n", IsPrime(0) ? "sim" : "nao"); | |
printf("1 é primo %s\n", IsPrime(1) ? "sim" : "nao"); | |
printf("3 é primo %s\n", IsPrime(3) ? "sim" : "nao"); | |
printf("2 é primo %s\n", IsPrime(2) ? "sim" : "nao"); | |
printf("4 é primo %s\n", IsPrime(4) ? "sim" : "nao"); | |
printf("5 é primo %s\n", IsPrime(5) ? "sim" : "nao"); | |
printf("7 é primo %s\n", IsPrime(7) ? "sim" : "nao"); | |
printf("9 é primo %s\n", IsPrime(9) ? "sim" : "nao"); | |
return 0; | |
} | |
/*Fonte: http://stackoverflow.com/a/1538653/5429980 | |
* Retorna 1 para numeros primo ou 0 para numeros que nao sao primos.*/ | |
int IsPrime(unsigned int number) { | |
if (number <= 1) return 0; // se o numero for menor ou igual a 1 então nao é primo. | |
unsigned int i; | |
for (i = 2; i * i <= number; i++) { | |
if (number % i == 0) return 0; | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment