How to compare two prime number php?

Asked By 10 points N/A Posted on -
qa-featured

Hello experts,

How to compare two prime number PHP? I am making a PHP code that will perfectly compare two prime numbers, can you give me some ways to get this done or any website and video tutorial that will probably help me to discover the coding technique.

Thank you.

SHARE
Best Answer by JOY Schilling
Answered By 10 points N/A #187683

How to compare two prime number php?

qa-featured

Hi Markusa,

PHP prime number comparison will be the same as of numbers. For this you can use one of the following operators.

1. x == y Equal True if x is equal to y.

2. x === y Identical True if x is equal to y, and they are of same type.

3. x!= y Not equal True if x is not equal to y.

4. x <> y Not equal True if x is not equal to y.

You can read more on:

Thank you.

Answered By 0 points N/A #187685

How to compare two prime number php?

qa-featured

Hello,

You can see the code number given below and compare yourself, you can take help from https://www.w3schools.com/ and alison.com

<?PHP

// is_prime(number) checks if a number is a prime number or not

function is_prime($i) {

if($i % 2 != 1) return false;

$d = 3;

$x = sqrt($i);

while ($i % $d != 0 && $d < $x) $d += 2;

return (($i % $d == 0 && $i != $d) * 1) == 0 ? true : false;

}

// example: show all prime numbers between $start and $end

$start = 0;

$end = 1000;

for($i = $start; $i <= $end; $i++)

{

if(is_prime($i))

{

echo '

'.$i.' ';

}

}

?>

Thanks.

Best Answer
Best Answer
Answered By 5 points N/A #187687

How to compare two prime number php?

qa-featured

Hi,

You want to compare two prime number. I am telling you the complete algorithm to compare prime numbers and code snippets in PHP.
 
Algorithm (From Wikipedia):
 
1. Create a list of consecutive integers from two to n: (2, 3, 4, …, n), 
2. Initially, let p equal 2, the first prime number, 
3. While enumerating all multiples of p starting from p2, strike them off from the original list, 
4. Find the first number remaining on the list after p (it's the next prime); let p equal this number, 
5. Repeat steps 3 and 4 until p2 is greater than n. 
6. All the remaining numbers in the list are prime. 
 
Code Snippets:
 
In this code we have used following Functions.
IsPalindrome
IsInteger
IsEven
IsPositive
IsPerfectSquare
IsPalindromicPrime
 
<?PHP
    function IsInteger($number)
    {
        $result = preg_match('/^-?[0-9]+$/', $number, $matches);
        return $result;
    }
 
    function IsEven($number)
    {
        return (($number % 2) == 0);
    }
     
    function IsPositive($number)
    {
        return $number >= 0;
    }
     
    function IsPerfectSquare($number)
    {
        return sqrt($number) == (int)sqrt($number);
    }
     
    function IsPalindrome($number)
    {
        return $number == strrev($number);
    }
     
    function PrimesFromErathosthenes($number)
    {
        $number = $number + 2; //Otherwise the actual number wouldn't be included in array of numbers
        $nlist = array();
        $sqrt = ceil(sqrt($number));
 
        $nlist[] = 2;
        $nlist[] = 3;
        for($i = 5 ; $i < $number ; $i += 2)
        {
            if($i % 3 != 0)
            {
                $nlist[] = $i;
            }
        }
         
        for($i=2,$divisor=5; $divisor < $sqrt ; $i++) 
        {
            $count = count($nlist);
            $j = $i*$i;
            $nlist2 = array_slice($nlist,0,$j);
             
            for( ; $j < $count ; $j++)
            {
                $num = $nlist[$j];
                if($num % $divisor != 0)
                {
                    $nlist2[] = $num;
                }
            }
            unset($nlist);
            $nlist = $nlist2;
            $divisor = $nlist[$i+1];
        }
         
        $primes = $nlist;
        return $primes;
    }
     
    function IsPrime($number, $primeSource, $displayOption)
    {
        if(!IsInteger($number))
        { 
            return false;
        } 
        else
        { 
            $number = (int) $number;
        }
        if(IsEven($number)){return false;}
         
        $primes;
         
        $IsInPrimesArray = false;
         
        if($primeSource == 'Sieve')
        {
            $primes = PrimesFromErathosthenes($number);
            if(in_array($number, $primes))
            {
                $IsInPrimesArray = true;
            }
            else
            {
                $IsInPrimesArray = false;   
            }
        }
         
        if($primeSource == 'File')
        {
            $primes = GetFirst100000PrimesFromFile();
            $number = "$number"."rn";
            if(in_array($number, $primes))
            {
                $IsInPrimesArray = true;
            }
            else
            {
                $IsInPrimesArray = false;
            }
        }
         
        if($displayOption == 'ShowPrimes')
        {
            echo "primes ".implode(" ", $primes)."<br />";
        }
         
        return $IsInPrimesArray;
    }
     
    function IsPalindromicPrime($number, $primeSource, $displayOption)
    {
        if(!IsPositive($number) || $number > 1318699)
        {
            return false;
        }
        if(!IsPalindrome($number))
        {
            return false;
        }
        else
        {
            if(IsPrime($number, $primeSource, $displayOption))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
     
    function GetFirst100000PrimesFromFile()
    {
        $primes = file('First100000Primes.txt');
        return $primes;
    }
     
?>
 
You can download the code file also. Please Find the attachment.
Answered By 0 points N/A #187688

How to compare two prime number php?

qa-featured

Hi,

Run this code to your localhost.

It will check whether number is prime or not.

Make one file named “insert.php”

<form method=POST action=”check.php”>

<input type="text" name="no">

<input type="submit" value="check">

</form>

Other file named “check.php”

<?php

$c = 2;

        $n=$_POST[‘no’];

   for ( $c = 2 ; $c <= $n – 1 ; $c++ )

   {

      if ( $n%$c == 0)

      {

         echo "not prime";

         break;

      }

   }

   if ( $c == $n )

{     

      echo "prime";

}

?>

Related Questions