Home  >  Article  >  Backend Development  >  How to write whether a number is prime in php

How to write whether a number is prime in php

尚
Original
2019-10-28 13:37:493200browse

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>";

How to write whether a number is prime in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn