jdm95lude Posted March 3, 2008 Share Posted March 3, 2008 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>" ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94120-need-help-with-prime-number-programm/ Share on other sites More sharing options...
dual_alliance Posted March 3, 2008 Share Posted March 3, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/94120-need-help-with-prime-number-programm/#findComment-482226 Share on other sites More sharing options...
sasa Posted March 3, 2008 Share Posted March 3, 2008 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>" ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/94120-need-help-with-prime-number-programm/#findComment-482231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.