Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Function to Check Prime Number
#1
Advertisment
Here is the best prime number program in PHP using for loop. It is quite simple and easy PHP function to check prime numbers. PHP prime number generator function.
PHP Code:
function is_prime($number){
 
   // 1 is not prime
 
   if $number == ) {
 
       return false;
 
   }
 
   // 2 is the only even prime number
 
   if $number == ) {
 
       return true;
 
   }
 
   // square root algorithm speeds up testing of bigger prime numbers
 
   $x sqrt($number);
 echo 
$x."==x<br>";
 
   $x floor($x);
 
   for $i $i <= $x ; ++$i ) {
 
       if $number $i == ) {
 
           break;
 
       }
 
   }
 
   
    if
$x == $i-) {
 
       return true;
 
   } else {
 
       return false;
 
   }

Advertisment
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Prime Number Program in PHP w3schools Kelly 2 2,553 10-05-2018, 05:44 PM
Last Post: kim kardashian
  Can I Return Multiple Values From a Function In PHP emily008 0 2,065 02-24-2015, 06:43 AM
Last Post: emily008

Forum Jump:


Users browsing this thread: 1 Guest(s)