Home >Backend Development >PHP Problem >How to write whether a number is prime in php
Prime numbers: Prime numbers are also called prime numbers. A natural number greater than 1 that is not divisible by other natural numbers except 1 and itself is called a prime number.
Recommended: php server
Determine whether a number is prime:
/** * 判断一个数是否是质数 * @param int $num 所要判断的数 */ function is_prime($num) { for ($i = 2; $i < $num; $i++) { if ($num % $i == 0) { return $num . "不是质数"; } } return $num . "是质数"; } echo is_prime(7)."<br>"; echo is_prime(9)."<br>"; echo is_prime(10)."<br>";
The above is the detailed content of How to write whether a number is prime in php. For more information, please follow other related articles on the PHP Chinese website!