To determine whether a number is prime

Source: Internet
Author: User
Tags pow
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 &nbsp;&nbsp; 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 ";
}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.