Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Function to Check Prime Number
Sponsered Link
#1
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;
 
   }

Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Prime Number Program in PHP w3schools Kelly 0 89 08-29-2017, 02:27 AM
Last Post: Kelly
  Can I Return Multiple Values From a Function In PHP emily008 0 1,391 02-24-2015, 06:43 AM
Last Post: emily008

Forum Jump:


Users browsing this thread: 1 Guest(s)