Skip to content

Instantly share code, notes, and snippets.

@RodriAndreotti
Created October 1, 2016 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RodriAndreotti/5110404cf8db3ff3646e66bee4fbc63e to your computer and use it in GitHub Desktop.
Save RodriAndreotti/5110404cf8db3ff3646e66bee4fbc63e to your computer and use it in GitHub Desktop.
Verifica se um número é primo ou não.
<?php
$numero = 101;
if($numero % 2 == 0){
// Nenhum número par, exceto 2 é primo
if($numero == 2){
echo 'É um número primo';
}
else{
echo 'Não é um número primo';
}
}
else{
$ePrimo = true;
for($i = 2; $i < $numero; $i++){
if($numero % $i == 0){
$ePrimo = false;
}
}
if($ePrimo) echo "É um número primo";
else echo "Não é um número primo";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment