Skip to content

Instantly share code, notes, and snippets.

@krmgns
Created December 1, 2021 21:40
Show Gist options
  • Save krmgns/1a85ce1afc4dc425c53d2aca98ebadcd to your computer and use it in GitHub Desktop.
Save krmgns/1a85ce1afc4dc425c53d2aca98ebadcd to your computer and use it in GitHub Desktop.
Prime number check.
<?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";
@mariia993
Copy link

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