10 Replies - 1037 Views - Last Post: 30 May 2012 - 04:39 PM Rate Topic: -----

#1 lobita4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-May 12

PHP problem with isPrime problem.

Posted 28 May 2012 - 08:31 PM

What I want my code to do is to show if a number is prime or not. The integer in the range of 3 1000 for the number. If the user enters a valid integer, display Yes or No in the input tag on the form.
It does not show any error messages, but, it will not show anything. It just clears the numbers! Please help me!

<?php
	function isPrime($number)
	{
		$prime = true;
		$div = 2;
		do
		{
			if ($number % $div == 0)
				$prime = false;
			else
				$div++;
		}
		while ($div < $number && $prime == true);
		return $prime;
	}
	$errorCount = 0;
	$prime = isPrime($_POST['number'], "Number");
{

function isValid($number, &$errorMessage)
{
if (empty($number))
{
$errorMessage = "Enter something.";
return false;
}
else if ($number != intval($number))
{
$errorMessage = "Enter an integer.";
return false;
}
else if ($number < 3 || $number > 1000)
{
$errorMessage = "Enter a number between 3 and 1000";
return false;
}
else
return true;
}	
}
	// some page level variables
	$number = "";
	$prime = "";
	$errorMessage = "";
	
	
	if (count ($_POST) > 0)

?>

<html>
  <head>
  </head>
  
  <body>
    <form id="primeForm" name="primeForm" method="post" action="primeStart.php">
      <p>Enter the number here:&nbsp;&nbsp;
      <input type="text" id="number" name="number" 
        value="<?php echo $number ?>" />
		<span style="color:red;"><?php echo $errorMessage ?></span>
      </p>
      <p><input type="submit" value="Is It A Prime?" name="submit" /></p>
      <p>Answer:&nbsp;
      <input type="text" id="prime" name="prime" 
        value="<?php echo $prime ?>"/></p>
    </form>
  </body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: PHP problem with isPrime problem.

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: PHP problem with isPrime problem.

Posted 28 May 2012 - 10:25 PM

why should it display something? you reset any output variables to empty strings just before the output.
Was This Post Helpful? 0
  • +
  • -

#3 lobita4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-May 12

Re: PHP problem with isPrime problem.

Posted 29 May 2012 - 12:24 PM

So, how would I fix it? I'm sorry, I'm just starting off with this. What part of the code do I modify to make it appear?
Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,676
  • Joined: 30-January 09

Re: PHP problem with isPrime problem.

Posted 29 May 2012 - 07:03 PM

You should be using the Sieve of Erastosthenes to determine primality. In other words, change line 13 from this:
while ($div < $number && $prime == true);


...to this...
while ($div < round(sqrt($number), 0, PHP_ROUND_HALF_DOWN) && $prime == true);



The thinking goes like this... if the number is composite, then it has to be divisible by a number less than itself. We need only check numbers up to the square root, since if the number is divisble by a number > the square root, then it must also be divisible by a number < the square root.

This post has been edited by e_i_pi: 29 May 2012 - 07:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 lobita4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-May 12

Re: PHP problem with isPrime problem.

Posted 29 May 2012 - 08:29 PM

If I do that, it gives me many errors, and I don't think that's the reason why it isn't showing me anything. It just goes back to a blank.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: PHP problem with isPrime problem.

Posted 29 May 2012 - 09:00 PM

You should actually do something with the results of the function call.
<?php
	// some page level variables
	$number = "";
	$prime = "";
	$errorMessage = "";
	
	function isPrime($number)
	{
		$prime = true;
		$div = 2;
		do
		{
			if ($number % $div == 0)
				$prime = false;
			else
				$div++;
		}
		while ($div < $number && $prime == true);
		return $prime;
	}
	$errorCount = 0;
	$prime = isPrime($_POST['number']);
if($prime) echo "PRIME<br>"; else echo "NOT PRIME<br>";

?>

<html>
  <head>
  </head>
  
  <body>
    <form id="primeForm" name="primeForm" method="post" action="primeStart.php">
      <p>Enter the number here:&nbsp;&nbsp;
      <input type="text" id="number" name="number" 
        value="<?php echo $number ?>" />
		<span style="color:red;"><?php echo $errorMessage ?></span>
      </p>
      <p><input type="submit" value="Is It A Prime?" name="submit" /></p>
      <p>Answer:&nbsp;
      <input type="text" id="prime" name="prime" 
        value="<?php echo $prime ?>"/></p>
    </form>
  </body>
</html>




...and it helps to not void the results before using them!
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: PHP problem with isPrime problem.

Posted 29 May 2012 - 09:08 PM

Quote

You should be using the Sieve of Erastosthenes to determine primality.

On a technicality, the Sieve of Eratosthenes is used to generate primes, rather than to test primality. You're right about checking up to the sqrt(n) though. :)
Was This Post Helpful? 0
  • +
  • -

#8 lobita4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-May 12

Re: PHP problem with isPrime problem.

Posted 30 May 2012 - 04:30 PM

Well, I managed to fix it like I wanted to at the start, but, now I have another issue. Guess I'll post another one again.
Was This Post Helpful? 0
  • +
  • -

#9 lobita4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-May 12

Re: PHP problem with isPrime problem.

Posted 30 May 2012 - 04:36 PM

Let me explain this code. I need this code to check if a particular integer is Prime or Not Prime. It does manage to call it and shows "yes" or "no", depending on weather it is or not a prime number. The other thing I want this code to do is validate the number. Like in my code, you need to enter something, it must be an integer, and be between 3-1000. Now, for some reason, I do not know what to do:( I need to validate this, and my code is correct. I just need to make sure it appears, and shows the message. Can someone please help me? Here is all my code:


<?php
	function isPrime($number)
	{
		$prime = true;
		$div = 2;
		do
		{
			if ($number % $div == 0)
				$prime = false;
			else
				$div++;
		}
		while ($div < $number && $prime == true);
		return $prime;
	}
	
{

function isValid($number, &$errorMessage)
{
		if (empty($number))
		{
			$errorMessage = "Enter something.";
			return false;
		}
		else if ($number != intval($number))
		{
			$errorMessage = "Enter an integer.";
			return false;
		}
		else if ($number < 3 || $number > 1000)
		{
			$errorMessage = "Enter a number between 3 and 1000";
			return false;
		}
		else
				return true;
}

}
$number = "";
     $prime = "";
		$errorMessage = "";
	if (count ($_POST) > 0)
      {
       $number = $_POST["number"];
      if(isPrime($number))
       $prime="Yes";
        else
       $prime="No";
      }	
	

?>

<html>
  <head>
  </head>
  
  <body>
    <form id="primeForm" name="primeForm" method="post" action="primeStart.php">
      <p>Enter the number here:&nbsp;&nbsp;
      <input type="text" id="number" name="number" 
        value="<?php echo $number ?>" />
		<span style="color:red;"><?php echo $errorMessage ?></span>
      </p>
      <p><input type="submit" value="Is It A Prime?" name="submit" /></p>
      <p>Answer:&nbsp;
      <input type="text" id="prime" name="prime" 
        value="<?php echo $prime ?>"/></p>
    </form>
  </body>
</html>



I meant to write: Whether not weather...sorry for my misspell.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: PHP problem with isPrime problem.

Posted 30 May 2012 - 04:38 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#11 lobita4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-May 12

Re: PHP problem with isPrime problem.

Posted 30 May 2012 - 04:39 PM

Well, this time it's a different issue, that's why. Before it wasn't properly showing my isPrime, and not it is my errorMessage. I think it's something different.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1