Archive

Posts Tagged ‘detect’

PHP Function To Detect A Prime Number

April 9th, 2009 1 comment

A prime number is a number which has exactly two distinct number divisors: 1 and itself. So if you take the number 11, it can only be divided to get a whole number if it is divided by 1 or 11. If any other number is used then a fraction is always found.

The following function uses a method called trial division to detect if a number is prime or not.

function is_prime($number)
{
    // 1 is not prime
    if ( $number == 1 ) {
        return false;
    }
    // 2 is the only even prime number
    if ( $number == 2 ) {
        return true;
    }
    // square root algorithm speeds up testing of bigger prime numbers
    $x = sqrt($number);
    $x = floor($x);
    for ( $i = 2 ; $i <= $x ; ++$i ) {
        if ( $number % $i == 0 ) {
            break;
        }
    }
    
    if( $x == $i-1 ) {
        return true;
    } else {
        return false;
    }
}

The function first detects if the number is 1 (not prime) or if it is two (prime). These are two exceptions to the rules that follow and must be caught before proceeding. The function divides the number by all numbers less than or equal to the square root of that number. If any of the divisions come out as an integer, then the original number is not a prime. Otherwise, it is a prime.

Here is an example bit of script that finds all of the prime numbers between 0 and 1,000,000.

$start = 0;
$end =   1000000;
for($i = $start; $i <= $end; $i++)
{
    if(is_prime($i))
    {
        echo '<strong>'.$i.'</strong>, ';
    }
}

Obviously this takes a little while to run!

Also, this function is only useful if you want to check integers, if your number is higher than the maximum value of an integer PHP will use a float to store the number, which causes false positives. To find the maximum value of an integer on your system use the following code.

echo PHP_INT_MAX;

Categories: PHP Tags: , , , , ,

Does A String In PHP Contain A Number?

December 24th, 2007 No comments

The is_numeric() function in PHP can be used to see if a number contained in a string is numeric. The function returns true is the variable can be parsed into a string, otherwise it returns false. Here are some examples:

is_numeric('five') // returns false
is_numeric(123); // returns true
is_numeric('123'); // returns true
is_numeric(-123); // returns true
is_numeric('-123'); // returns true
is_numeric('123.4'); // returns true
is_numeric('1,234'); // returns false

Notice that if your number has a thousand separator in it the function will return false. In this case you need to use the str_replace() function to strip out the commas before passing the value into is_numeric(0).

is_numeric(str_replace(',','','1,234')); // returns true

Remember that by default all GET and POST variables will be passed to PHP as strings so this function is useful when checking form input as it can verify that a value is a number before you go on to check the value of that number.

PHP also has a variety of other functions to allow you to check that a variable is a number of a specific type. These are is_bool(), is_float() (also known as is_double() or is_real(), is_int() (also known as is_integer()), and is_long().