C++ 求两个x1,x2之间的素数之和,找不到错误?
我需要找出两个数之间的素数之和,比如说x1和x2,但是我无法检测出有什么错? 例如,如果我输入3和9,我将得到15,但我得到133C++ 求两个x1,x2之间的素数之和,找不到错误?,c++,function,loops,C++,Function,Loops,我需要找出两个数之间的素数之和,比如说x1和x2,但是我无法检测出有什么错? 例如,如果我输入3和9,我将得到15,但我得到133 #include <iostream> using namespace std; int prime(int n1, int n2) { int count =0; bool prime = true; for (n1; n1 < n2; n1++) { for (int i = 2; i <
#include <iostream>
using namespace std;
int prime(int n1, int n2)
{
int count =0;
bool prime = true;
for (n1; n1 < n2; n1++)
{
for (int i = 2; i < n1; i++)
{
if (n1 % i == 0) {
prime = false;
continue;
}
else
count++;
}
}
return count;
}
int main()
{
int n1, n2;
cout << " Enter values for n1 and n2 (n1 must be smaller than n2): ";
cin >> n1>>n2;
cout << " Sum of prime numbers from " << n1 << " and till " << n2 << " inclusively : " << prime(n1, n2) << endl;
system("pause");
return 0;
}
#包括
使用名称空间std;
整数素数(整数n1,整数n2)
{
整数计数=0;
布尔素数=真;
对于(n1;n1n1>>n2;
cout你的素数函数不合适。这应该是这样的
int prime(int n1, int n2) {
int sum = 0;
for (n1; n1 < n2; n1++) {
bool prime = true;
for (int i = 2; i < n1; i++) {
if (n1 % i == 0) {
prime = false;
break;
}
}
if( prime ) { // current n1 is prime
sum = sum + n1;
}
}
return sum;
}
int素数(int n1,int n2){
整数和=0;
对于(n1;n1
如果n1为素数,则不会添加任何内容。我非常相信每个函数有一个循环。这是一个很好的例子。您的内部循环检查一个数字是否为素数,因此它实际上应该是一个函数本身:
bool is_prime(int n);
int sum_primes_between(int n1, int n2)
{
int sum = 0;
for (; n1 <= n2; n1++)
{
if (is_prime(n1))
sum += n1;
}
return count;
}
到目前为止,没有一个答案符合包容性的要求。
下面是一个修正版本,包括检查素性的优化算法:
#include <iostream>
#include <cmath>
using namespace std;
bool is_prime(int n) {
// handle special cases
if (n <= 1) {
return false;
}
if (n <= 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
/* because we covered multiples of 2 and 3 we can reduce the number of checks
drastically */
for(int i = 5, r = sqrt(n); i =< r; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
int sum_of_primes(int n1, int n2) {
int sum = 0;
// loop from n1 up to n2
for ( ;n1 <= n2; n1++) {
if (is_prime(n1)) {
sum += n1;
}
}
return sum;
}
int main() {
cout<<"The sum of primes between 2 and 15 is: "<<sum_of_primes(2,15)<<endl;
return 0;
}
#包括
#包括
使用名称空间std;
布尔是素数(整数n){
//处理特殊情况
如果(n步骤thr’a调试器…内部for循环似乎有问题,“如果我输入3和9,我会得到15,但我得到133!”当我尝试时,我得到16
内部循环中的代码在几个方面都是错误的。您可能想要生成isPrime(int num)函数并将主要检查从内部循环中分离出来。作为旁白-不要相信用户的输入。假设他们会做坏事,并且失败。你可以使用i
而不是i
来加快检查速度。我有点困惑我的错误只是因为我有“bool prime=true;”在循环之外?对不起,应该是i我可能会错过一些东西,但是在is_prime(),如果我理解得很好,第二个测试应该是n%(i+2)
,而不是(n%i)+2
。此外,它应该是
#include <iostream>
#include <cmath>
using namespace std;
bool is_prime(int n) {
// handle special cases
if (n <= 1) {
return false;
}
if (n <= 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
/* because we covered multiples of 2 and 3 we can reduce the number of checks
drastically */
for(int i = 5, r = sqrt(n); i =< r; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
int sum_of_primes(int n1, int n2) {
int sum = 0;
// loop from n1 up to n2
for ( ;n1 <= n2; n1++) {
if (is_prime(n1)) {
sum += n1;
}
}
return sum;
}
int main() {
cout<<"The sum of primes between 2 and 15 is: "<<sum_of_primes(2,15)<<endl;
return 0;
}