topic:
Write a form, you can enter a number, submitted to determine whether the number is a "prime". If it is, the output "number xx is prime", otherwise the output "number xx is not prime."
Tip: The concept of prime numbers is: It is only divisible by 1 and itself--within the range of integers greater than 1.
The code is as follows:
<?php
$jieguo 1 = "";
$number = "";
if ($_post) {
$number =$_post["Shuzi"];
$jieguo 1=$_post["Jieguo"];
$n = 0;
for ($i =1; $i <=pow ($number, 1/2); $i + +) {
if ($number% $i = = 0) {
$n + +;
}
}
if ($n ==1) {
$jieguo 1 = "entered number". $number. " Is prime ";
}else{
$jieguo 1 = "entered number". $number. " Not prime ";
}
}
?>
<form action= "" method= "POST" >
Please enter a number: <input type= "text" name= "Shuzi" value= "<?php echo $number;?>"/>
<input type= "Submit" value= "Start judgment" ><br/>
Knot Fruit: <input type= "text" name= "Jieguo" value= "<?php echo $jieguo 1;?>"/>
</form>
Here are three ways to distinguish prime numbers:
The first of these methods
$number 1=19;
$n 1=0;
For ($i =1 $i <= $number 1; $i + +) {
if ($number 1 $i = = 0) {
$n 1++;
}
}
if ($n 1==2) {
echo $number 1. " Is prime ";
}else{
echo $number 1. " Not prime ";
}
echo "The second method
$number 2=19;
$number 22= $number 2/2;
$n 2=0;
For ($i =1 $i <= $number 2/2; $i + +) {
if ($number 2 $i = = 0) {
$n 2++;
}
}
if ($n 2==1) {
echo $number 2. " Is prime ";
}else{
echo $number 2. " Not prime ";
}
echo "The third method
$number 3=19;
$n 3=0;
For ($i =1 $i <=pow ($number 3,1/2); $i + +) {
if ($number 3 $i = = 0) {
$n 3++;
}
}
if ($n 3==1) {
echo $number 3. " Is prime ";
}else{
echo $number 3. " Not prime ";
}
?>