C++ 查找素数的程序意外停止
我编写了一个非常简单的程序来查找用户指定范围内的素数。但是我遇到了一个问题。当程序到达一个复合数时,它只是停止打印素数。我试图了解它为什么会停止,但我无法理解它到底出了什么问题,可能是因为我对编程还不熟悉。不管怎样,这是代码C++ 查找素数的程序意外停止,c++,for-loop,numbers,C++,For Loop,Numbers,我编写了一个非常简单的程序来查找用户指定范围内的素数。但是我遇到了一个问题。当程序到达一个复合数时,它只是停止打印素数。我试图了解它为什么会停止,但我无法理解它到底出了什么问题,可能是因为我对编程还不熟悉。不管怎样,这是代码 #include <iostream> using namespace std; int main() { int y; int range; cout << "Please enter the range. \n";
#include <iostream>
using namespace std;
int main()
{
int y;
int range;
cout << "Please enter the range. \n";
cin >> range;
for (y = 2; y <= range; y++)
{
int result;
for (int x = 1; x < y - 1; x++)
{
int prime = y - x;
if (y%prime != 0)
{
}
else
{
result = 0;
}
}
if (result != 0)
{
cout << y << " is a prime number. \n";
}
}
}
#包括
使用名称空间std;
int main()
{
int-y;
整数范围;
范围;
对于(y=2;y,正如Brian Gradin指出的,我看到的唯一问题是应该将结果初始化为非零整数
int result = 1;
只有在此初始化之后,才能在for循环之后进行有效检查,检查结果是否已更改为零
如果没有初始化,对该变量值的任何访问都会导致未定义的行为
编辑:
为了完整起见,我应该补充其他人的建议,即更标准的方法是:
for (y = 2; y <= range; y++)
{
bool isPrime = true;
// The following loop should be changed to loop through the Sieve of primes
for (int x = 2; x*x < y ; x++) // You need to loop only till sqrt(y)
{
if (y%x == 0) // if you found a factor
{
isPrime = false;
break;
}
}
if ( isPrime )
{
cout << y << " is a prime number. \n";
// and add this to the sieve of primes.
}
}
for(y=2;y正如Brian Gradin指出的,我看到的唯一问题是应该将结果初始化为非零整数
int result = 1;
只有在此初始化之后,才能在for循环之后进行有效检查,检查结果是否已更改为零
如果没有初始化,对该变量值的任何访问都会导致未定义的行为
编辑:
为了完整起见,我应该补充其他人的建议,即更标准的方法是:
for (y = 2; y <= range; y++)
{
bool isPrime = true;
// The following loop should be changed to loop through the Sieve of primes
for (int x = 2; x*x < y ; x++) // You need to loop only till sqrt(y)
{
if (y%x == 0) // if you found a factor
{
isPrime = false;
break;
}
}
if ( isPrime )
{
cout << y << " is a prime number. \n";
// and add this to the sieve of primes.
}
}
对于(y=2;yresult
可以在不初始化的情况下使用。您应该将结果初始化为非零整数,int result=1;对于此任务(打印低于某个限制的所有素数),您几乎肯定要使用Eratosthenes筛(或其某些变体)您可以使用if(y%prime==0){result=0;break;}来整理内部循环的主体
因为一旦知道它是一个复合数,就不需要继续检查。result
可以在不初始化的情况下使用。您应该将result初始化为非零整数,int result=1;对于此任务(打印低于某个限制的所有素数),您几乎肯定要使用Eratosthenes筛(或其某些变体)您可以使用if(y%prime==0){result=0;break;}来整理内部循环的主体
因为一旦你知道它是一个复合数字,你就不需要继续检查了。我知道你在那里做了什么,但解释OP或其他可能会有所帮助。请将结果
初始化为某个东西,这让我很紧张。不是零。就像我说的-我知道他做了什么。但简单地发布代码是没有帮助的。你没有这是真的吗?result
是否会是0
之外的任何东西?这个答案仍然是错误的。result
需要在外循环内部初始化,或者逻辑只工作一次。我知道你在那里做了什么,但可能有助于解释OP或其他请将result
初始化为某个东西,这会让我感到不安RVUS。不是零。就像我说的-我知道他做了什么。但是简单地发布代码是没有帮助的。你没有测试过这个,是吗?result
会是0
以外的任何东西吗?这个答案仍然是错误的。result
需要在外部循环内初始化,或者逻辑只工作一次。我可以确认o一旦进行了此更改,原始代码将起作用+1@AvZ,请接受这个答案,如果有帮助的话,这样其他人就可以看到这个问题已经有了答案。当然,要感谢答案的作者:)(我不是答案的作者)我可以确认,一旦进行了此更改,原始代码将正常工作+1@AvZ,请接受这个答案,如果有帮助的话,这样其他人就可以看到这个问题已经有了答案。当然,要感谢答案的作者:)(我不是答案的作者)
for (y = 2; y <= range; y++)
{
bool isPrime = true;
// The following loop should be changed to loop through the Sieve of primes
for (int x = 2; x*x < y ; x++) // You need to loop only till sqrt(y)
{
if (y%x == 0) // if you found a factor
{
isPrime = false;
break;
}
}
if ( isPrime )
{
cout << y << " is a prime number. \n";
// and add this to the sieve of primes.
}
}