PHP – List of Prime Numbers to Given Range

The following php example helps you to get the list of prime numbers from the given range.

Code Example:

<?php
$num1 = 2;
$num2 = 100;
 
$col = 0;
echo "<h1>List of prime numbers from $num1 to $num2 </h1>";
echo '<table width="60%" border="1"><tr>';
foreach ( Prime ( $num1, $num2 ) as $item ) {
if ($col >= 10) {
echo '</tr><tr>';
$col = 0;
}
echo "<td> $item </td>";
$col ++;
}
echo '</tr></table>';
 
function Prime($num1, $num2) {
	$prime_numbers = array ();
	while ( $num1 < $num2 ) {
		$isprime=true;
		for($i = 2; $i <= sqrt ( $num1 ); $i ++) {
			if ($num1 % $i == 0)
				$isprime=false;
		}
	 
	if ( $isprime ) {
		$prime_numbers [] = $num1;
	}
	$num1 ++;
	}
	return $prime_numbers;
}
?>

Output:

List of prime numbers from 2 to 100
2 	3 	5 	7 	11 	13 	17 	19 	23 	29
31 	37 	41 	43 	47 	53 	59 	61 	67 	71
73 	79 	83 	89 	97 

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail