Home > Backend Development > PHP Problem > PHP determines whether a prime number is

PHP determines whether a prime number is

(*-*)浩
Release: 2023-02-27 12:10:02
Original
3561 people have browsed it

PHP determines whether a prime number is

Determine whether it is a prime number. A prime number refers to a natural number that has no other factors except 1 and itself among natural numbers greater than 1. (Recommended learning: PHP video tutorial)

<html> 
<?php
 
if (!empty($_POST)) {
    $num = $_POST["num"];
    $count = 0;
    for ($i = 1; $i <= $num; ++$i) {
        if ($num % $i == 0) {
            $count++;
        }
    }
} else {
    $num = "";
    $count = "";
}
 
?>
 
<form action="" method="post">
    <input type="text" name="num" value="<?php
    echo $num;
    ?>"/>
    <input type="button" name="tip" value="<?php
    if ($count == 2) {
        echo $num . "是素数";
    } else {
        echo $num . "不是素数";
    }
    ?>"/>
    <input type="submit" value="判断"/>
</form>
 
 
</html>
Copy after login

The above is the detailed content of PHP determines whether a prime number is. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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