php获取递归函数中的主要数字


php get the primary numbers in recursive function

我只是php的新手。目前我正在学习递归函数。所以我想知道如何在递归中得到从0到1000的所有素数。有人能告诉我怎么做吗?这也将帮助我了解递归函数。

你可以这样做。。

<?php
function dispPrime($i)
{
    if($i<=1000)
    {
        if(gmp_prob_prime($i)===2) // Checks the number for prime.
        {
            echo "$i is a Prime Number";
        }
        $i++;
        dispPrime($i);  // Recursive call (Function that calls itself)
    }
    else{ exit;}
}
dispPrime(0);
<?php
// Checks for prime numbers
function IsPrime($num) {    
    $No = 0 ;
    $Result  = 0 ; 
    for($Divisor = 2 ; $Divisor < $num; $Divisor++) {
        $Result = $num / $Divisor ; 
        if($Result != 1 && intval($Result) == $Result) { 
            $No = 1 ;
            break ; 
       } 
    }
    if($No != 1  )  {
        $Result = $num ; 
    }
    $No = 0;
    // If the only divisor is the number itself, it's prime 
    return ($Result == $num) ? 'Yes' : 'No' ; 
}  
for($i = 0; $i < 1000; $i++) {
    echo "<b> Testing number $i  : </b>"  ; 
    echo $i." is a prime number? ". IsPrime($i)."<br />";
}
  ?>