Prime number in PHP

  • Sumo

A prime number is a natural number/whole number/cardinal number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.
e.g. 2, 3, 5, 7, 11, 13, 17….etc are prime numbers; while 4, 6, 8, 9, 10, 12, 14, 15, 16….etc are composite numbers.

Program to determine if the supplied number is Prime or Composite using native bcmod() function.

<?php
$num = 26; // desired number to check
for ( $i = 2; $i <= $num - 1; $i++ ) {
if ( bcmod( $num, $i ) == 0 ) {
echo $num . " is composite number";
break;
}
}
if ( $num == $i )
echo $num . " is prime number";
?>

One Comment

  1. Howard says:

    hello!,I like your writiong very a lot! proportion we krep up a corrwspondence more approximately your article on AOL?
    I require ann expert onn this area to ssolve my problem.
    Maybe that’s you! Looking forward to look you.

    Feel free to surf to my weeb site:cheap echo of soul
    gold online (Howard)

Leave a Reply

Your email address will not be published. Required fields are marked *