Php prime number program with use of class
<?php
<?php
class find_p_number
{
# Function to check single number is Prime or not
static function isPrime($number)
{
# Start temporary number from 2 (1 is not a prime number)
$i = 2;
# 1 is not a prime number
if($number == 1)
return false;
# 2 is the only even prime number
if($number == 2)
return true;
# Check numbers other than 1 and 2
while($i <= sqrt($number))
{
if ($number % $i == 0)
return false;
$i++;
}
# If the execution has come to this point, means it is a prime Number :)
return true;
}
# Function to check Prime Numbers between two given numbers
static function betweenTwoNumbers($num1, $num2)
{
$prime_numbers = array();
# Check Prime Numbers between two given numbers
for($num1; $num1<$num2; $num1++)
{
# Check whether the current number is prime or not
if(self::isPrime($num1))
$prime_numbers[] = $num1;
}
# Returns the array of prime numbers between supplied numbers
return array_values(array_unique($prime_numbers));
}
}
?>
Comments
Post a Comment