Created
December 1, 2021 21:40
-
-
Save krmgns/1a85ce1afc4dc425c53d2aca98ebadcd to your computer and use it in GitHub Desktop.
Prime number check.
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
<?php | |
function is_prime(int|float $n): bool { | |
if ($n == 2) | |
return true; | |
if ($n < 2 or $n % 2 == 0) | |
return false; | |
for ($i = 3; $i <= sqrt($n); $i++) { | |
if ($n % $i == 0) | |
return false; | |
} | |
return true; | |
} | |
$a = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]; | |
$b = []; | |
for ($i = 0; $i < 72; $i++) { | |
if (is_prime($i)) { | |
$b[] = $i; | |
echo "$i "; | |
} | |
} | |
echo "\n"; | |
echo ($a === $b) ? "true" : "false"; |
what is $a is already defined?
sorry for црфе шы
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
црфе шы <= sqrt($n)
what if n* false?