Jump to content

Need help with prime number programm


jdm95lude

Recommended Posts

here is the link to the program it works fine unless you enter in the number 656 it repeats.

http://apollo1.occc.edu/sp0808/ssp_03/PrimeNumber.html

 

The following is my form code.

   <form action="PrimeNumber.php" method="get" >
  	  <p>Enter a number: <input type="text" name="number" id="number"/>
	   <input type="submit" name="submit" id="submit"/></p>
    </form>

 

the following is my php code

 

<?php
// variable n is equal to the id of number on PrimeNumber.html
$PrimeNum = $_GET['number'];

// Checks to see if the number entered is between 1-1000
if ( $PrimeNum == 0 || $PrimeNum >= 1000) 
{
	echo "<p>The number must between 1 and 999. Please try again.</p>";
	echo "<p><a href='PrimeNumber.html'>Go Back.</a></p>" ;

}

// Calculates to check and see if a number is prime. If it is not prime it displays an error message.
       for($i = 2; $i < sqrt($PrimeNum); $i = $i + 2){
              if ($PrimeNum % $i == 0){
		 		 echo "<p>The number you entered was not a prime number.</p>" ;
                     echo "<p><a href='PrimeNumber.html'>Go Back.</a></p>" ;
				 // sends the answer to a variable.
				 $notPrime = true;
              }
        }
	// if the number entered is prime it displays a message.
        if ($notPrime != true) {
		echo "<p>The number you entered is prime!</p>";
		echo "<p><a href='PrimeNumber.html'>Go Back.</a></p>" ;
	}
?>

Link to comment
Share on other sites

I can't test it at the moment, but from what l can see you need to do something like below

 

<?php
// variable n is equal to the id of number on PrimeNumber.html
$PrimeNum = $_GET['number'];

// Checks to see if the number entered is between 1-1000
// Make sure it also checks to see if the number is less then 0 
if ( $PrimeNum <= 0 || $PrimeNum >= 1000) 
{
echo "<p>The number must between 1 and 999. Please try again.</p>";
echo "<p><a href='PrimeNumber.html'>Go Back.</a></p>" ;
die(); // Make it so the script stops here and doesn't continue running
}

// Calculates to check and see if a number is prime. If it is not prime it displays an error message.
for($i = 2; $i < sqrt($PrimeNum); $i = $i + 2){
if ($PrimeNum % $i == 0){
echo "<p>The number you entered was not a prime number.</p>" ;
echo "<p><a href='PrimeNumber.html'>Go Back.</a></p>" ;
die(); //Like before, we know its not a prime number so there is no reason to continue processing
}else{
echo "<p>The number you entered is prime!</p>";
echo "<p><a href='PrimeNumber.html'>Go Back.</a></p>" ;
die(); // like before, except we know now that this is a prime number
}
}
?>

 

Hopefully it works... :D

Link to comment
Share on other sites

your script doesn't right find prime numbers

try number 15

change to

<?php
// variable n is equal to the id of number on PrimeNumber.html
$PrimeNum = $_GET['number'];

// Checks to see if the number entered is between 1-1000
// Make sure it also checks to see if the number is less then 0 
if ( $PrimeNum <= 0 || $PrimeNum >= 1000)
{
echo "<p>The number must between 1 and 999. Please try again.</p>";
echo "<p><a href='PrimeNumber.html'>Go Back.</a></p>" ;
die(); // Make it so the script stops here and doesn't continue running
}
//is number 1 prime?
// Calculates to check and see if a number is prime. If it is not prime it displays an error message.
for($i = 2; $i <= sqrt($PrimeNum); $i = $i + 1){
if ($PrimeNum % $i == 0){
	echo "<p>The number you entered was not a prime number.</p>" ;
	echo "<p><a href='PrimeNumber.html'>Go Back.</a></p>" ;
	die(); //Like before, we know its not a prime number so there is no reason to continue processing
}
}
echo "<p>The number you entered is prime!</p>";
echo "<p><a href='PrimeNumber.html'>Go Back.</a></p>" ;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.