the king

URBANMYTHCAFE.COM

Home | Mathematics | Actuarial Study Blog

Primality"

This page uses php to identify if an integer is prime or composite. It takes integers of up to 12 digits.

The algorithm used is just the simple one of dividing the integer (N) in question by 2, 3, 4, ... , sqrt(N). When a divisor is found, the function terminates and returns composite.

Here is the code:

 
 function primep( $N)
// divide by each of the integers up to sqrt N
// bail out and return f as soon as the remainder equals 0
{
	$stop = floor(sqrt ($N));
    for ( $m=2; $m <= $stop; $m++)
    {
        if ($N%$m ==0) return "composite";
    }
    return "prime";
}
 
 

Home | Mathematics | Actuarial Study Blog

©Copyright 2003-2017 Jimothi Difonzo