简体   繁体   English

查找给定范围内的最高质数

[英]Find the highest prime number in a given range

I need to find the highest prime number in a given range. 我需要找到给定范围内的最高质数。
Here is my code which works for 0-100 but if I give 0-125 it is showing prime number as 125. 这是我的代码,适用于0-100,但如果我给0-125,则显示质数为125。

<?php
    $flag=0;
    $b=125;
    for($i=$b;$i>=0;$i--)
    {
        if($i%2!=0)
        {
            for($b=3;$b<10;$b++)
            {
                if($flag==0)
                {
                    echo('<br>');
                    if($i%$b!=0)
                    {
                        echo('highest prime number is'.$i);
                        $flag=1;
                        break;
                    }
                    elseif ($i%$b==0)
                    {
                        break;
                    }
                }
            }
        }
    }
?>

In the above code I have taken the range from 0-125 在上面的代码中,我采用了0-125的范围

gmp_nextprime() gmp_nextprime()

<?php
$start = 125;
$stop = 0;
for($x=$start;$x>=$stop;$x--){
    if(($prime = gmp_intval(gmp_nextprime($x)))<$start){
        echo 'The highest prime is '.$prime;
        break;
    }
}?>

thanks for your reply Samuel Cook ..but i need without using 'gmp_nextprime()' function.. iamgetting message 'Call to undefined function gmp_intval() in C:\\wamp\\www\\highprime.php on line 5' – user1659450 16 mins ago 谢谢你的答复塞缪尔·库克..但我需要不使用'gmp_nextprime()'函数.. iamgetting消息'第5行在C:\\ wamp \\ www \\ highprime.php中调用未定义函数gmp_intval()'– user1659450 16分钟前

Since you are having difficultly getting gmp functions to work then you can use 由于您很难使用gmp functions ,因此可以使用

Example 1 : None GMP Function 示例1:无GMP功能

$range = range(125, 0); // create the range
foreach ( $range as $v ) {
    if (isPrime($v)) {
        printf('highest prime number is %d', $v);
        break;
    }
}

If you are able to get gmp working then you can use gmp_prob_prime Function Used 如果您能够使gmp正常工作,则可以使用gmp_prob_prime函数

Example 1 : Using gmp function 示例1:使用gmp函数

foreach ( $range as $v ) {
    if (gmp_prob_prime($v) == 2) {
        printf('highest prime number is %d', $v);
        break;
    }
}

Output 输出量

highest prime number is 113

Functions Used 使用的功能

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;
}

Yup the problem is algorithmic... 是的,问题是算法上的...

1) You'll need to check up till sqrt($b) ie 11 in this case 1)您需要检查直到sqrt($b)在这种情况下为11

2) The $flag logic is kinda messed up, no use changing the flag then breaking out right after... 2) $flag逻辑有点混乱,更改标志然后在出现后立即中断是没有用的...

<?php
        $flag=0;
        $b=125;
        $sq=sqrt($b); 

        for($i=$b;$i>=0;$i--)
        {

            if($i%2!=0)
            {
                    for($b=3;$b<=$sq;$b++)
                    {
                            if($i%$b!=0)
                            {
                                $flag=1;
                            }
                            elseif($i%$b==0)
                            {
                                $flag=0;
                                break;
                            }
                    }
            if($flag==1){ 
                echo('highest prime number is '.$i);  
                break;
            }
        }
    }
?>

The simplest method you can use is dividing a number starting from 3 with an Array filled by a 2, if modulus (%) gives you an an answer else than 0, it's prime. 您可以使用的最简单方法是将数字从3开始除以以2填充的数组,如果模数(%)给出的答案不是0,则为质数。 Then after you got a working code think what can I make better ? 然后,在获得有效代码后,请考虑我能做些什么呢?

  • You could say I want to ignore even numbers! 您可能会说我想忽略偶数! so why iterate over than at all ? 那么为什么要比完全迭代呢? just use i = i + 2 (and start by using 3 as seed prime) 只需使用i = i + 2(并使用3作为种子素数开始)
  • I divide through too many numbers! 我除以太多数字! do you really need to check 21%11, 21%13, 21%17, 21%19 ? 您是否真的需要检查21%11、21%13、21%17、21%19? They are always smaller than 2 so lets ignore primes > half of it 它们总是小于2,因此让我们忽略素数>一半的素数
  • Optimize more ? 优化更多? how would limit using a root as maximum in your code affect it ? 限制在代码中最大使用根是如何影响根的? and Why ? 为什么?
  • Even more ? 还有吗? why couldn't I eliminate all non-primes beforehand and check what's left ? 为什么我不能事先消除所有非素数并检查还剩下什么?

Try to apply these on your code, or just google a working version. 尝试将这些应用到您的代码上,或者只是谷歌搜索工作版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM