Last active
August 7, 2025 14:52
-
-
Save DominikStyp/f78cc6845e0655ec080abc9cb6ad29f6 to your computer and use it in GitHub Desktop.
PHP Prime Numbers Algorithm
This file contains hidden or 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 | |
$from = 13; | |
$to = 300; | |
$primes = [2, 3, 5, 7, 11, 13]; | |
// & due to no array copy, and only pass | |
function divides_by_prime(array &$primes, int $number) { | |
// optionally if primes wouldn't be segregated from smallest so largest, or some missing | |
// we could limit the script by adding this for optimization: | |
// $numSqrt = sqrt($number); | |
foreach($primes as $prime){ | |
// and here we could then do | |
// if($prime > $numSqrt) return false; | |
if($number % $prime === 0) return true; | |
} | |
return false; | |
} | |
for($i = $from; $i < $to; $i++) { | |
if(! divides_by_prime($primes, $i)) { | |
$primes[] = $i; | |
} | |
} | |
print_r(implode(",", $primes)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment