Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP函数isPrime()不工作_Php - Fatal编程技术网

PHP函数isPrime()不工作

PHP函数isPrime()不工作,php,Php,请帮助我用代码检查表中的数字是否为素数:下面是我的代码。我使用了isPrime函数,但是我得到了一个错误,当我调用表中的函数时,它返回单词“istime” 您丢失了整个基本检查,甚至没有在第三列中打印任何内容 另外,似乎有人在第一次编辑时删除了您的代码,并在第二次编辑时对其进行了更改。不知怎的,我有一份第一次编辑的副本,可以做一个例子 这是代码中的一个工作示例: <?php $x="num1"; $y="num2"; if(isset($_POST['Submit'])) {

请帮助我用代码检查表中的数字是否为素数:下面是我的代码。我使用了isPrime函数,但是我得到了一个错误,当我调用表中的函数时,它返回单词“istime”


您丢失了整个基本检查,甚至没有在第三列中打印任何内容

另外,似乎有人在第一次编辑时删除了您的代码,并在第二次编辑时对其进行了更改。不知怎的,我有一份第一次编辑的副本,可以做一个例子

这是代码中的一个工作示例:

<?php 

$x="num1"; $y="num2"; 
if(isset($_POST['Submit'])) 
{ 
    $start=$_POST['num1']; $end=$_POST['num2']; 
    echo "<table width='250' cellpadding='2' cellspacing='4' border='1'>"; 
    echo "<tr><td>Number</td><td>Odd or Even?</td><td>Prime?</td></tr>"; 
    for ($start;$start<=$end;$start++) 
    { 
        $answer=$start; $number=$answer; $check="text"; $num=$answer; 
        if($number%2==0) 
            { $check="Even"; } 
        else 
            { $check="Odd"; } 
        $prime = (isPrime($number)? 'Yes':'No');
        echo "<tr><td>$answer</td><td>$check</td><td>$prime</td></tr>"; 
    } 
    echo "</table>"; 
} 
function isPrime($num) {
    //1 is not prime. See: http://en.wikipedia.org/wiki/Prime_number#Primality_of_one
    if($num == 1)
        return false;

    //2 is prime (the only even number that is prime)
    if($num == 2)
        return true;

    /**
     * if the number is divisible by two, then it's not prime and it's no longer
     * needed to check other even numbers
     */
    if($num % 2 == 0) {
        return false;
    }

    /**
     * Checks the odd numbers. If any of them is a factor, then it returns false.
     * The sqrt can be an aproximation, hence just for the sake of
     * security, one rounds it to the next highest integer value.
     */
    for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
        if($num % $i == 0)
            return false;
    }

    return true;
}
?>
试试这个

<?php 
  $x="num1"; 
  $y="num2"; 

  if(isset($_POST['Submit'])) { 

      $start=$_POST['num1']; 
      $end=$_POST['num2']; 

      echo "<table width='250' cellpadding='2' cellspacing='4' border='1'>"; 
        echo "<tr><td>Number</td><td>Odd or Even?</td><td>Prime?</td></tr>"; 

      for ($start;$start<=$end;$start++) { 

          $answer=$start;  
          $number=$answer; 
          $check="text"; 
          $num=$answer; 

          if($number%2==0) 
          { 
              $check="Even"; 
          } 
          else 
          { 
              $check="Odd"; 
          } 

        if(isPrimeNumber($number))
        {   
            $pirme_status = "Yes";  
        }
        else
        {
            $pirme_status = "No";   
        }   

       echo "<tr><td>$answer</td><td>$check</td><td>$pirme_status</td><</tr>";



    }  echo "</table>"; 

}

写问题时请分享你的代码页面底部有预览区。PHP没有isPrime()函数,你必须写一个…函数
isPrime()
在哪里?此基本计算不正确。。。不要使用
$num==1
$num==2
只需使用
$num<2
,因为您没有检查负数。此素数计算是正确的。我无法将
$num==1
$num==2
更改为
$num<2
,因为有两个不同的结果。1不是质数,2是质数。此外,负数在默认情况下不是素数。如果接受负数,则需要为负整数返回false,如果($num<0 | | |!is_int($num))返回false,则非整数
If($num<0 | |!is_int($num))返回false。但大多数情况下,只有在期望正整数时才使用此函数。我可以假设OP的
$num
总是正整数,因为他想要输出。
<?php 
  $x="num1"; 
  $y="num2"; 

  if(isset($_POST['Submit'])) { 

      $start=$_POST['num1']; 
      $end=$_POST['num2']; 

      echo "<table width='250' cellpadding='2' cellspacing='4' border='1'>"; 
        echo "<tr><td>Number</td><td>Odd or Even?</td><td>Prime?</td></tr>"; 

      for ($start;$start<=$end;$start++) { 

          $answer=$start;  
          $number=$answer; 
          $check="text"; 
          $num=$answer; 

          if($number%2==0) 
          { 
              $check="Even"; 
          } 
          else 
          { 
              $check="Odd"; 
          } 

        if(isPrimeNumber($number))
        {   
            $pirme_status = "Yes";  
        }
        else
        {
            $pirme_status = "No";   
        }   

       echo "<tr><td>$answer</td><td>$check</td><td>$pirme_status</td><</tr>";



    }  echo "</table>"; 

}
function isPrimeNumber($number)
{
    $flag = true;
    $max_count = ceil($number/2);
    for($i=2; $i<$max_count; $i++)
    {
        if($number%$i==0)
        {
            $flag = false;
                    break;
        }
    }
    return $flag;
}
?>