Open In App

How to Find Prime Number using Sieve of Eratosthenes in PHP ?

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A prime number is defined as a number that only can be divided by 1 and that number. The number N is prime if it is only divisible by 1 and number N. If the number is divisible by any other number then it is a composite number. For example, number 7 is a prime number and number 12 is a composite number.

Input: 7
Output: 7 is a prime number.

Input: 12
Output: 12 is a composite number.

A composite number can be written as a multiple of two or more numbers. For example, number 12 can be written as follows:

12= 12*1 = 2*6 = 3*4 = 4*3 = 6*2 = 12*1

Two methods can be used to find if the given number is prime or not.

Brute force Method

If we divide given number by 2 to N-1, and if it will give the remainder as 0, then we can say that the number is not prime. We can use this property to check whether the number is prime or not.

We can do it using the following steps.

  • Loop from 2 to number N-1.
  • Check the remainder after dividing N by the current number.
  • If we get 0 as an answer at any stage then we will return false.
  • Else at the end of the loop we will return true.

Following is the code for above approach.

PHP




<?php
  
function isPrime($number) {
    if ($number <= 1) {
        return false;
    }
  
    for ($i = 2; $i < $number; $i++) {
        if ($number % $i == 0) {
            return false;
        }
    }
  
    return true;
}
  
function checkNumberType($number) {
    if (isPrime($number)) {
        echo $number . " is a prime number." . PHP_EOL;
    } else {
        echo $number . " is a composite number." . PHP_EOL;
    }
}
  
// Example usage:
$input1 = 7;
$input2 = 12;
  
checkNumberType($input1);
checkNumberType($input2);
  
?>


Output

7 is a prime number.
12 is a composite number.

Time Complexity: O(N)

Space Complexity: O(1)

Sieve of Eratosthenes

12= 12*1 = 2*6 = 3*4 = 4*3 = 6*2 = 12*1

If we observe the factors of number 12 given above carefully we can find a pattern. After half the numbers will repeat themselves. In case of 12 the numbers which are factors of twelve before half are 12 , 1, 2 , 6 , 3 ,4 and the factors after half are also the same. Therefore there if not any need to check all the numbers till N-1 instead we can check only till square root of N. This approach will give us a better time complexity.

Following are the steps that can be followed for this approach.

  • Loop from 2 to number square root on N.
  • Check the remainder after dividing N by the current number.
  • If we get 0 as an answer at any stage then we will return false.
  • Else at the end of the loop we will return true.

PHP




<?php
  
function isPrimeOptimized($number) {
    if ($number <= 1) {
        return false;
    }
  
    for ($i = 2; $i <= sqrt($number); $i++) {
        if ($number % $i == 0) {
            return false;
        }
    }
  
    return true;
}
  
function checkNumberTypeOptimized($number) {
    if (isPrimeOptimized($number)) {
        echo $number . " is a prime number." . PHP_EOL;
    } else {
        echo $number . " is a composite number." . PHP_EOL;
    }
}
  
// Example usage:
$input1 = 7;
$input2 = 12;
  
checkNumberTypeOptimized($input1);
checkNumberTypeOptimized($input2);
  
?>


Output

7 is a prime number.
12 is a composite number.

Time Complexity: O(sqrt(N))

Space Complexity: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads