-1

My html document has the following form:

<form action="PrimeNumber.php" method="post">
Enter a number to determine if it is a prime number: <input type="text" name="numb" size="10">
<input type="submit" value="Check for Primeness">
</form>

Edit: Using a different code now but can't get it to echo a statement no matter what I do. Anyone know how I can make it echo is a prime number or is not.

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['numb'])) {
        $num = $_POST['numb'];

        function isPrime($num) {
            if($num == 1) {
                return false;
            }

            if($num == 2) {
                return true;
            }

            if($num % 2 == 0) {
                return false;
            }

            for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
                if($num % $i == 0)
                    return false;
            }
            return true;
        }
    }
}
?>
Eric Tobias
  • 3,225
  • 4
  • 32
  • 50
user3529629
  • 11
  • 1
  • 2
  • available on `(PHP 5 >= 5.2.0)` is it your case? – ChoiZ Apr 13 '14 at 18:38
  • 1
    You have to install the GMP package. – The Blue Dog Apr 13 '14 at 18:38
  • If I install the package will it still work for people viewing it who don't have the package? Thanks BTW. – user3529629 Apr 13 '14 at 18:39
  • Of course it will, it's server-side. – The Blue Dog Apr 13 '14 at 18:40
  • I don't know what is your issue, though if you are eager to check if number is prime or not here is the algorithm written in `JavaScript` though probably you find it interesting. [Check Out](http://stackoverflow.com/questions/23045746/understanding-number-functions-in-javascript/23045785#23045785) – nanobash Apr 13 '14 at 18:43
  • But even after you install the package are you sure that you will get the desired result with the above code? You need to review your code. – Vagabond Apr 13 '14 at 18:48

5 Answers5

1

simple and easy to understand code, and working as well. do little changes according to your requirement, like assign input text value to $a variable and you good to go.

$a = 51;
for($i=2; $i < $a; $i++){
    if($a % $i == 0){
        echo "Numberis not prime.";
        break;
    }
    else{
        echo "Number is not prime.";
        break;
    }
}
sayyed tabrez
  • 116
  • 1
  • 1
  • 11
1

A simple function to check is prime number:

function is_prime($p) {
    return ($p > 1) &&  (($p%2 >= 1) && ($p%3 >= 1) && ($p%5 >= 1)) ||  in_array($p, [2,3,5]);
}
Said Sabir
  • 101
  • 4
1
function checkPrime($num){
      $isPrime = true;
    
    for($i = 2; $i <= sqrt($num); $i++)
    {
        if($num % $i == 0) 
        {
          $isPrime = false;
        }
    }
    if($isPrime == true)
    {
        return "$num is prime number";
    }else{
        return "$num  is not prime number";
    }
}

echo checkPrime(29); 

The output is

29 is prime number

For More Details

Roy2012
  • 11,755
  • 2
  • 22
  • 35
Pradip
  • 11
  • 3
0

You may use the gmp package and correct your code because I got an output as 11 not being a prime number or use the following function as an alternative.

function isPrime($num) {
    if($num == 1) {
        return false;
    }

    if($num == 2) {
        return true;
    }

    if($num % 2 == 0) {
        return false;
    }

    for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {

        if($num % $i == 0)

            return false;
    }
    return true;
}

More information is available at the following page.

Vagabond
  • 877
  • 1
  • 10
  • 23
0

A simple function to help you find out if a number is a prime number

<?php 
     function fn_prime($number) {
        $i = 2; $result = TRUE;
        while($i < $number) {
            if(!($number%$i)) {
                $result = FALSE;
            }
            $i++;
        }
        return $result;
     }
?>
Chigozie Orunta
  • 448
  • 3
  • 13