简单的质数程序...我的代码/脚本有什么问题?
我想知道我可以在调试或发现程序中的错误时获得一些帮助。目标是获取用户输入,然后显示从输入到零的素数、最大素数到最小素数。
问题是输出包括用户输入,该输入本身可能是也可能不是素数,并且多次重复素数:( 另外,我想知道为什么不包括2?
我的代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int prime, division, input;
cout << "Please enter a number you wish to count down prime numbers from." << endl;
cin >> input;
for (prime = input ; prime >=2 ; prime-- )
{
for (division = 2 ; division < prime ; division++ )
{
if (prime % division == 0)
{
break;
}
else
{
cout << prime << " ";
}
}
}
return 0;
}
我的输出:
请输入您想要从中倒数素数的数字。 15
15 13 13 13 13 13 13 13 13 13 13 13 11 11 11 11 11 11 11 11 11 9 7 7 7 7 7 5 5 5 3
感谢那些提供帮助的人!
I am wondering I can get some help at debugging or spotting the error in my program. The objective is to obtain user input and then display primes from input to zero, greatest prime to lowest.
Problem is the output includes the user input which may or may not be a prime number in itself, and repeats primes several times :(
Also, I am wondering why 2 isn't included?
My code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int prime, division, input;
cout << "Please enter a number you wish to count down prime numbers from." << endl;
cin >> input;
for (prime = input ; prime >=2 ; prime-- )
{
for (division = 2 ; division < prime ; division++ )
{
if (prime % division == 0)
{
break;
}
else
{
cout << prime << " ";
}
}
}
return 0;
}
My output:
Please enter a number you wish to count down prime numbers from.
15
15 13 13 13 13 13 13 13 13 13 13 13 11 11 11 11 11 11 11 11 11 9 7 7 7 7 7 5 5 5 3
Thanks for those who help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个判断输入数字是否为素数的程序。任何数字都可以被始终小于它的数字完全整除。
如果是素数,它们可以完全被 1 和它本身整除;
所以我使用了一个计数器变量,它计算一个数字完全被一个或小于它的数字整除的次数。
对于素数,计数始终为 2,对于其他数,计数将大于 2。
计数将是 1 为 1...
所以程序如下......
打印所有小于输入给定数字的素数;代码如下:
This is a program which tells whether the input number is prime or not.Any number is completely divisible by a number which is always less than it.
In case of prime numbers they are completely divisible by 1 and itself;
So i have used a counter variable which counts how many times a number is completely divisible by a number or numbers less than it.
The count will be always 2 for prime numbers and count will be more than two for others.
Count will be 1 for one...
So the program is as follows....
To print all the prime numbers less than the number given as input; code is as follows:
试试这个代码
Try this code
对于第一个问题:
对于9,当
division == 2
时,9 % 2 ==1
,所以打印9。而不是你应该有一个标志值来表示判断一个数是否是质数,确定它是质数后打印该数。对于 2,编辑:当
prime == 2
时,并且因为division
prime
是内循环执行的条件,内循环退出而不运行。所有功劳归于马龙!For the first problem:
for 9, when
division == 2
,9 % 2 ==1
, so 9 is printed.Rather than You should have a flag value to denote if a number is prime or not, and print the number after you are sure it is prime.For 2, EDIT: when
prime == 2
, and becausedivision < prime
is the condition that the inner loop get executed, the inner loop exits without running. All credits go to Marlon!检查你的内部 for 循环。每次运行循环时,初始化器都会重置为 2。然后对可疑素数执行 Mod 2。如果你得到 0,你真正知道的是这个数字是偶数。这可能会让你很快说这个数字不是素数,但 2 mod 2 是 0,并且 2 是素数。循环结束后,“除法”变为 3。15 mod 3 为 0。此时,您将跳出循环,但 15 不是质数。检查您的确定素数的算法。您可以让它检查已知的素数,但这不够动态。还有其他方法,其中之一围绕确定可疑素数的平方根。最后,您可以进行一些良好的旧(长)纸笔调试。
Check your inner for loop. Each run through the loop, your initializer gets reset to 2. Then you do Mod 2 on the suspected prime. If you get 0, all you really know is that the number is even. That might lead you to quickly say that the number isn't prime, but 2 mod 2 is 0, and 2 is prime. After the loop, "division" becomes 3. 15 mod 3 is 0. At that point, you break out of the loop, but 15 is NOT prime. Check your algorithm for determining primes. You could have it check against known primes, but that's not dynamic enough. There are other methods, one of which revolves around determining the suspected prime number's square root. Finally, you could do some good old (long) paper-pencil debugging.
你的内循环不正确。它不打印素数,只打印奇数。它在第一次迭代期间检查一个数字是否可以被 2 整除,并且偶数总是可以被整除。因此,如果数字是偶数,内部循环总是会中断,但如果是奇数,它会打印数字,并继续这样做,直到循环中断或终止。
让我们尝试用一个例子来说明这一点,外循环在 9 处。内循环将检查它是否能被 2 整除,因为它不能被 2 整除,所以它会打印出数字并再次继续。下一次迭代将检查它是否能被 3 整除,因为它会被打破。
并尝试使用函数来检查数字是否为素数,这使其更加模块化。这是一个稍微优化的版本......
Your inner loop is not correct. Its not printing primes, just odd numbers. It checks whether a number is divisible by 2 during first iteration and an even number will always be divisble. So the inner loop always breaks out if the number is even but it prints the number if odd and continues doing so till the loop breaks or terminates.
Lets try to get this with an example, the outer loop is at 9. The inner loop will check if its divisble by 2, since its not it'll print out the number and continue again. Next iteration will check whether its divisible by 3, since it is it'll break out.
And try using a function to check whether a number is prime that makes it more modular. Here's a little optimized version...