Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.
<?php
function get_primes($min,$max) {
$primes = array();
for ( $x=$min; $x<=$max; $x++ ) {
//$x is every number between$ min and $max. Main loop
if ( $x == 2 ) { $primes[] = $x; } //2 is a prime
for ( $i=2; $i<$x; $i++ ) {
//$i is every integer such that 2<$i<$x
$remainder = ($x % $i);
if ( $remainder == 0 ) {
$i = $x; //Set $i=$x so that the loop reaches the end and goes on to next value of $x
continue; //Break current loop
} else if ( $i == ($x-1) ) {
$primes[] = $x; //Means this is the largest value of $i. So if we reached this far the number is prime
} else {
//Do nothing, let the loop go on
}
}
}
return $primes;
}
?>
This function should return primes between X and Y.
function prime($min,$max)
{
$prime=array();
for ($i=$min;$i<=$max;$i++)
{
$count=0;
for ($p=1;$p<=$i;$p++)
{
if ($i%$p==0)
{
$count++;
}
if ($count>2)
{
break;
}
}
if ($count==2)
{
$prime[]=$i;
}
}
return $prime;
}
does the same thing. I just think it reads better. on another note. you're function is faster..not entirely sure why not by much, just enough to bug me
In the end it will just come back to something like my function. Also, I don't use a variable $count at all so that may be saving some time (I don't know much about speed optmization so I'm not sure if it would make any difference though).
Verminox wrote:
To tell you the truth, I did not get you. Could you explain a bit more with the square root thing?
In case x is not a prime (thus a composite) then x can be factored in two numbers and at least one of these numbers is less than or equal to square (x).
Proof:
- We assume that x is not a prime, but composed by two numbers, name them v and w (thus x = v * w)
- We assume that both numbers are larger than square(x) (thus v > sqrt(x) and w > sqrt(x))
We know that v > sqrt(x) and w > sqrt(x)
=> v * w > sqrt(x) * sqrt(x)
=> v * w > x !!! impossibility
Therefor, the assumption that both v and w are larger than sqrt(x) cannot be true.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi here is my function, BUT it doesn't have minimum value!!!
It's faster than previous 2 functions, even when max is 5000 and minimum (not for this function) is 2500, but if (max - min) is small, then this function will be much slower.
It's checking if x is dividing with other prime numbers, not will all numbers until x
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
<?php
function get_primes($min,$max) {
$primes = array();
for ( $x=$min; $x<=$max; $x++ ) {
//$x is every number between$ min and $max. Main loop
if ( $x == 2 ) {
$primes[] = $x; //2 is a prime
} else if ( $x % 2 == 0 ) { //Even number greater than 2
continue; //Break Loop
}
for ( $i=2; $i<$x; $i++ ) {
//$i is every integer such that 2<$i<$x
$remainder = ($x % $i);
if ( $remainder == 0 ) {
$i = $x; //Set $i=$x so that the loop reaches the end and goes on to next value of $x
continue; //Break current loop
} else if ( $i == ($x-1) ) {
$primes[] = $x; //Means this is the largest value of $i. So if we reached this far the number is prime
} else {
//Do nothing, let the loop go on
}
}
}
return $primes;
}
?>
Edit: Well I guess this isn't any better than your idea digoide, either will work fine. And this function is so small and trivial that I don't think anybody would even want to optimize it any more.