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

How to write whether a number is prime in php

Release: 2023-02-27 21:04:02
Original
3266 people have browsed it

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>";
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template