PHP - check if number is prime

后端 未结 18 1393
野趣味
野趣味 2021-01-25 00:03

I\'m trying to create a function which checks whether the number is prime or not. BUT I want this function to echo to the user \'prime\' or \'NOT prime\' - and that\'s where my

相关标签:
18条回答
  • 2021-01-25 00:35

    Entire program is correct . But only you should do replace break with exit function .because break only used to exit loop which are enclosed with curly braces {} and exit are used to stop the execution of an entire script.

    class IsPrime
    {   
        function check($num)
        {
            for ($i = 2; $i < $num; $i++)
            {
                if ($num % $i == 0) 
                {
                    echo 'NOT prime';
                    break;
                }               
            }
            echo 'Prime';           
        }       
    }
    
    $x = new IsPrime();
    $x->check(4);
    
    0 讨论(0)
  • 2021-01-25 00:37
    Just use return:
    class IsPrime
    {       
    function check($num)
    {
        for ($i = 2; $i < $num; $i++)
        {
            if ($num % $i == 0) 
            {
                echo 'NOT prime';
                return; // that you need
            }               
        }
        echo 'Prime';           
    }       
    }
    
    $x = new IsPrime();
    $x->check(4);
    

    if/else notation
    return notation

    0 讨论(0)
  • 2021-01-25 00:39

    I made a similar one where a user types in a number and PHP checks if the number is prime or not.

    HTML

    <p>See if your number is a prime number</p>
    
    <form>
    
    <input type='number' name='number'>
    
    <input type='submit' value='Check!'>
    
    </form>
    

    PHP

    if ($_GET) {
    
        $i = 2;
    
        $isPrime = true;
    
        while ($i < $_GET['number']) {
    
            if ($_GET['number'] % $i == 0){
    
                // Number is NOT prime
                $isPrime = false;
    
            }
    
            $i++;
        }
    
        if ($isPrime){
    
            echo '<p>'.$i.' is a prime number!';
    
        } else {
    
            echo '<p>'.$i.' is NOT a prime number';
    
        }
    
    }
    

    Hopefully this works for you.

    0 讨论(0)
  • 2021-01-25 00:40

    That is my solution. If you put the condition in the loop and break after it, it's never going to finish with the check.

    $num = rand ( 1, 1000 );
    
    $notPri = null;
    
    for($check = 2; $check < $num; $check ++) {
    
          if ($num % $check == 0) {
              $notPri ++;
             }
           }
    
    if ($neEpri == 0) {
    
        echo $num . "<br>Prime!";
    
    } else {
         echo $num . "<br>Not a prime!";
       }
    
    0 讨论(0)
  • This could be a long procedure if the number is really a prime number. There is a shorter procedure as well. We need not run the for loop upto the number itself. Instead, we can run it upto the Highest Integer less than or equal to the square root of the number.

    class IsPrime
    {       
         function check($num)
         {
            $bCheck = True;
    
    
            $highestIntegralSquareRoot = floor(sqrt($num));
            for ($i = 2; $i <= $highestIntegralSquareRoot; $i++)
            {
                if ($num % $i == 0) 
                {
                     $bCheck = False;
                     break;
                }               
            }
           if $bCheck 
              echo 'Prime';           
           else
              echo 'NOT prime'; 
         }       
      }
    
     $x = new IsPrime();
     $x->check(97);
    

    Now, in the above, if we check check whether 97 is prime or not (actually, it is), then the loop need not run from 2 to 97, but only from 2 to 9. (Square root of 97 is 9.8488578018, and highest integer less than or equal to that is 9. Similarly, we can check for number 121 (this is not a prime number, as it is divisible by 11). The limit will be increased from 2 to 11 in a similar matter. And so on. Actually, we need to check the divisibility of the number by the smaller prime numbers in the vicinity, but that would be more complex. Hope this helps.

    0 讨论(0)
  • 2021-01-25 00:45
    $num = ceil(sqrt($num));
    
    $is_prime = true;
    for($j=3; $j<=$num; $j=$j+2){
        if($i%$j == 0){         
            $is_prime = false;
            break;
        }
    }
    
    if($is_prime){
        echo "No is Prime";
    }
    

    Note: Start loop from 2 as it is the only even prime no. Increment with 2 as no even no is a prime no.

    => Code for finding all prime no in range (2-100)

    $limit = 100; $arr = array(2);
    for($i=3; $i<=$limit; $i=$i+2){
    
        $num = ceil(sqrt($i));
    
        $is_prime = true;
        for($j=3; $j<=$num; $j=$j+2){
            if($i%$j == 0){         
                $is_prime = false;
                break;
            }
        }
    
        if($is_prime){
            $arr[] = $i;
        }
    }
    
    0 讨论(0)
提交回复
热议问题