PHP - check if number is prime

后端 未结 18 1368
野趣味
野趣味 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:46

    in 54 59 bytes:

    function is_prime($n){for($i=$n;--$i&&$n%$i;);return$i==1;}
    

    loops $i down from $n-1 until it finds a divisor of $n; $n is prime if that divisor is 1.


    add 10 bytes for much better performance:

    function is_prime($n){for($i=$n**.5|1;$i&&$n%$i--;);return!$i&&$n>1;}
    

    loops $i from (approx.) sqrt($n) to 1 looking for a divisor with a post-decrement on $i.
    If the divisor is 1, $i will be 0 at the end, and !$i gives true.

    This solition uses a trick: For $n=2 or 3, $i will be initialized to 1 → loop exits in first iteration. For larger even square roots ($n**.5|0), |1 serves as +1. For odd square roots, +1 is not needed because: if $n is divisible by root+1, it is also divisible by 2. Unfortunately, this can cost a lot of iterations; so you better


    add another 7 bytes for even better performance:

    function is_prime($n){for($i=~-$n**.5|0;$i&&$n%$i--;);return!$i&$n>2|$n==2;}
    

    $n=2 needs a special case here: inital $i=2 divides $n=2 → final $i=1 → returns false.

    Adding 1 to $n instead of the square root is enough to avoid failures; but:


    I did not count the iterations but only tested time consumption; and that only in TiO instead of a controlled environment. The difference between the last two versions was smaller than the deviation between several runs. Significant test results may be added later.

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

    The break is only breaking the for loop, use return; instead. it will exit the function

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

    First, don't make a mistake here:

    for ($i = 2; $i < $num; $i++)
    

    and then:

    if ($num % $i == 0) return false;
    

    2 % 2 equals 0 and then 2 will result as NOT prime.

    Next, you don't have to check even numbers, so after you check if $num == 2, better performance is:

    for ($i = 3; $i < $num/2; $i += 2)
    

    Notice $num/2 - you don't have to check beyond that point. And even better is:

    for ($i = 3; $i*$i <= $num; $i += 2)
    

    This is because when you check for division with 2 and 3 all other NON prime numbers are product of two (or more) prime numbers (e.g. 49 = 7*7 or 55 = 5*11). In the worst case scenario your $i pointer would reach a square root of $num (like in 49 = 7*7). That's why you check until $i*$i <= $num.

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

    You can find prime numbers and non prime numbers from 1 to your limit and its count.

    public function prime_checker($count){
      $counter=0;
      $no_prime=0;
      for($i=2 ; $i<=$count; $i++ ){
        for($j=2 ; $j<$i ; $j++ ){
          if($i % $j == 0){
            echo $i.'is not prime<br/>';
            $no_prime=$i;
            break;
          }
        }
        if($i != $no_prime){
          $prime_numbers[$counter]=$i;
          $counter=$counter+1;
        }    
      }
      echo '<br/>'.$counter.'prime numbers<br/><br/>';
      for($i=0 ; $i<$counter ; $i++ ){
        echo $prime_numbers[$i].' is  prime<br/>';
      }
    }
    
    0 讨论(0)
  • 2021-01-25 00:50

    Put else part otherwise it will always return Prime

        for ($i = 2; $i < $num; $i++)
    {
        if ($num % $i == 0)
        {
            echo 'NOT prime';
            break;
        }
        echo 'Prime';
    }
    
    0 讨论(0)
  • 2021-01-25 00:52

    try this

    class IsPrime
    {       
    function check($num)
    {
        for ($i = 2; $i < $num; $i++)
        {
            if ($num % $i == 0) 
            {
                echo 'NOT prime';
                break;
            }
            else
            {
                echo 'Prime';
            }
        }
        //echo 'Prime';           
    }       
    }
      $x = new IsPrime();
      $x->check(3);
    
    0 讨论(0)
提交回复
热议问题