Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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
素数检查c++;函数输出非';t素数 我正在制作一个C++程序,允许输入一个数字并检查它是否是素数。但是它说像9,15和21这样的数字是素数。我能得到一些帮助吗? 这很令人困惑。下面是我的函数,用于检查它是否为素数: bool isPrime(int num) { int w = 2; while (w <= num) { if (w % num == 0) { return false; } else if (w < num){ w = w + 1; } if (w == num) { w = 0; return true; } } } bool-isPrime(int-num){ int w=2; 而(w_C++_Primes - Fatal编程技术网

素数检查c++;函数输出非';t素数 我正在制作一个C++程序,允许输入一个数字并检查它是否是素数。但是它说像9,15和21这样的数字是素数。我能得到一些帮助吗? 这很令人困惑。下面是我的函数,用于检查它是否为素数: bool isPrime(int num) { int w = 2; while (w <= num) { if (w % num == 0) { return false; } else if (w < num){ w = w + 1; } if (w == num) { w = 0; return true; } } } bool-isPrime(int-num){ int w=2; 而(w

素数检查c++;函数输出非';t素数 我正在制作一个C++程序,允许输入一个数字并检查它是否是素数。但是它说像9,15和21这样的数字是素数。我能得到一些帮助吗? 这很令人困惑。下面是我的函数,用于检查它是否为素数: bool isPrime(int num) { int w = 2; while (w <= num) { if (w % num == 0) { return false; } else if (w < num){ w = w + 1; } if (w == num) { w = 0; return true; } } } bool-isPrime(int-num){ int w=2; 而(w,c++,primes,C++,Primes,我相信你想要 if(num%w==0) 不 如果(w%num==0)在这里,此代码可能有助于'bool isPrime(int-num) intw=2; 虽然(w已经发现了实际的bug(w%num而不是num%w),但是还有一些额外的提示: 你的代码太复杂了 while (w <= num) // why <=? w == num is irrelevant, in worst // case, it will lead to false neg

我相信你想要
if(num%w==0)

如果(w%num==0)

在这里,此代码可能有助于'bool isPrime(int-num)

intw=2;

虽然(w已经发现了实际的bug(
w%num
而不是
num%w
),但是还有一些额外的提示:

你的代码太复杂了

while (w <= num)  // why <=? w == num is irrelevant, in worst
                  // case, it will lead to false negatives (num % num == 0)!
{
    if (num % w == 0) // (already fixed!)
    {
         return false;
    }
    else if (w < num)
    {
        w = w + 1;
    }

    if (w == num) // as you increment by 1, this will always be false unless
                  // previous test failed - so simply use else instead
    {
        w = 0;
        return true; 
    }
}
当你意识到所有大于3的素数都可以写成6n+1或6n+5表示自然n时,你就可以获得到的额外速度。或者更进一步,所有大于5的素数都可以写成30n+m,m在{1,7,11,13,17,19,23,29}中。这就是所谓的

这简单地理解为:

  • 2的轮因式分解(cfr.Aconcagua):如果n不能被2整除,那么n不能被2的任意倍数整除
  • 轮因式分解6=2x3:如果n不能被2整除,则n不能被2的任意倍数整除;如果n不能被3整除,则n不能被3的任意倍数整除
  • 30=2x3x5的轮因式分解:见上文
因此,实施6的轮因式分解,可以快速得出:

if (num == 1)     return false;
if (num  < 4)     return true;
if (num % 2 == 0) return false;
if (num % 3 == 0) return false;
int w = 5;
while (w*w <= num)
{
    if(num % (w-2) == 0) return false;
    if(num % w     == 0) return false;
    w += 6;
}
return true;
if(num==1)返回false;
如果(num<4)返回true;
if(num%2==0)返回false;
if(num%3==0)返回false;
int w=5;

while(w*w)应该是
num%w
这可能会有所帮助。旁注:您检查的数字太多了;您可以减少:
while(w*w
。然后在第一个if块之后删除任何内容,只需留下
w=w+1;
或更短的:
++w;
并将
返回true;
放在while循环之后。只有一半的代码,但仍在执行相同的操作……进一步减少不必要的测试的一种非常简单的方法:在循环之前的单独if中检查%2,然后从开始<代码> w=3 < /C++ >在循环中执行<代码> W+=2 < /COD>(有方法跳过3, 5的倍数,其他也一样,但这不会像跳过2倍的多……)那么简单。谢谢。
while (w < num)
{
    if (w % num == 0)
    {
        return false;
    }
    /*else*/ if (w < num) // however, this check is repeated in the while
                          // loop anyway; no need to do the work twice 
    {
        ++w; // shorter...
    }
    else
    {
        // w = 0; // obsolete, we will be destroyed afterwards anyway...
        return true; 
    }
}
while (w < num)
{
    if (w % num == 0)
    {
        return false;
    }
    ++w; // at some point, will reach w == num and the loop won't be re-entered
}
// we did not leave the loop prematurely (-> non-prime), so we are prime:
return true;
if(num % 2 == 0)
    return false;
int w = 3;
while (w*w <= num)  // be aware that I had an error here in my comment
                    // to the question - cannot fix it any more, though...
{
    if(num % w == 0)
        return false;
    w += 2;
}
return true;
if (num == 1)     return false;
if (num  < 4)     return true;
if (num % 2 == 0) return false;
if (num % 3 == 0) return false;
int w = 5;
while (w*w <= num)
{
    if(num % (w-2) == 0) return false;
    if(num % w     == 0) return false;
    w += 6;
}
return true;