1

我不确定我是否应该在这里问或程序员,但我一直在试图弄清楚为什么这个程序不能工作,虽然我发现了一些错误,但它仍然返回“x 不是质数”,即使它是。

#include <iostream>
using namespace std;


  bool primetest(int a) {
 int i;
 //Halve the user input to find where to stop dividing to (it will remove decimal point as it is an integer)
 int b = a / 2;
 //Loop through, for each division to test if it has a factor (it starts at 2, as 1 will always divide)
 for (i = 2; i < b; i++) {
     //If  the user input has no remainder then it cannot be a prime and the loop can stop (break)
     if (a % i == 0) {
           return(0);
           break;
     }
     //Other wise if the user input does have a remainder and is the last of the loop, return true (it is a prime)
              else if ((a % i != 0) && (i == a -1)) {
          return (1);
          break;
     }
 }   
}

 int main(void) {
int user;
cout << "Enter a number to test if it is a prime or not: ";
cin >> user;
if (primetest(user)) {
                   cout << user << " is a prime number.";
}
else  {
      cout << user<< " is not a prime number.";
}
cout << "\n\nPress enter to exit...";
getchar();
getchar();
return 0;
}

对不起,如果这太本地化了(在这种情况下,你能建议我应该在哪里问这样的具体问题吗?)

我应该补充一点,我对 C++(和一般编程)非常陌生

这只是为了测试功能和控制。

4

7 回答 7

5

i永远不能等于a - 1- 你只能达到b - 1. ba/2,那永远不会导致匹配。

这意味着将返回 1 的循环结束条件永远不会为真。

在素数的情况下,您会跑到循环的末尾。这会导致未定义的行为,因为您在那里没有return声明。Clang 发出警告,没有任何特殊标志:

example.cpp:22:1: warning: control may reach end of non-void function
      [-Wreturn-type]
}
^
1 warning generated.

如果您的编译器没有警告您,则需要打开更多警告标志。例如,-Wall在使用 GCC 时添加会给出警告:

example.cpp: In function ‘bool primetest(int)’:
example.cpp:22: warning: control reaches end of non-void function

总体而言,您的素数检查循环比它需要的要复杂得多。假设您只关心a大于或等于的值2

bool primetest(int a)
{
    int b = sqrt(a); // only need to test up to the square root of the input

    for (int i = 2; i <= b; i++)
    {
        if (a % i == 0)
           return false;
   }

   // if the loop completed, a is prime
   return true;
}

如果要处理所有int值,只需if (a < 2) return false;在开头添加一个即可。

于 2013-07-02T17:37:27.017 回答
1

你可以通过检查直到平方根来做到这一点。但下面是对您的代码进行一些修改以使其工作。

#include <iostream>
using namespace std;
bool primetest(int a) {
int i;
//Halve the user input to find where to stop dividing to (it will remove decimal point as it is an integer)
int b = a / 2;
//Loop through, for each division to test if it has a factor (it starts at 2, as 1 will always divide)
for (i = 2; i <= b; i++) {
 //If  the user input has no remainder then it cannot be a prime and the loop can stop (break)
 if (a % i == 0) {
       return(0);

 }
}
//this return invokes only when it doesn't has factor
return 1;   
}

int main(void) {
  int user;
  cout << "Enter a number to test if it is a prime or not: ";
  cin >> user;
  if (primetest(user)) {
               cout << user << " is a prime number.";
  }
  else  {
     cout << user<< " is not a prime number.";
  }

return 0;

}

于 2013-07-02T17:57:04.357 回答
1

你的逻辑不正确。您正在使用这个表达式(i == a -1)),正如卡尔所说,这永远不会是真的。

例如:-

 If a = 11

 b = a/2 = 5  (Fractional part truncated)

所以你正在循环运行直到i<5. 所以i永远不能等于,a-1因为在这种情况下 i 的最大值将为 4,而 的值为a-110

于 2013-07-02T17:44:45.457 回答
0

看一下这个:

//Prime Numbers generation in C++
//Using for loops and conditional structures
#include <iostream>
using namespace std;

int main()
{
int a = 2;       //start from 2
long long int b = 1000;     //ends at 1000

for (int i = a; i <= b; i++)
{

 for (int j = 2; j <= i; j++)
 {
    if (!(i%j)&&(i!=j))    //Condition for not prime
        {
            break;
        }

    if (j==i)             //condition for Prime Numbers
        {
              cout << i << endl;

        }
 }
}
}
于 2013-09-23T19:46:09.770 回答
0
main()
{
    int i,j,x,box;
    for (i=10;i<=99;i++)
    {
        box=0;
        x=i/2;
        for (j=2;j<=x;j++)
            if (i%j==0) box++;
        if (box==0) cout<<i<<" is a prime number";
        else cout<<i<<" is a composite number";
        cout<<"\n";
        getch();
    }
}
于 2013-10-23T16:14:05.510 回答
0

一种方法是使用筛分算法,例如Eratosthenes 筛子。这是一种非常快速的方法,效果非常好。

bool isPrime(int number){
  if(number == 2 || number == 3 | number == 5 || number == 7) return true;
  return ((number % 2) && (number % 3) && (number % 5) && (number % 7));
}
于 2015-01-15T00:42:45.660 回答
0

这是在任何用户输入数字之前查找素数的完整解决方案。

#include <iostream.h>
#include <conio.h>
using namespace std;

main() 
{
 int num, i, countFactors;
 int a;
 cout << "Enter number " << endl;
 cin >> a;

 for (num = 1; num <= a; num++)
 {
  countFactors = 0;
  for (i = 2; i <= num; i++)
  {
   //if a factor exists from 2 up to the number, count Factors
   if (num % i == 0)
   {
    countFactors++;    
   }
  }

  //a prime number has only itself as a factor
  if (countFactors == 1)
  {
   cout << num << ", ";
  }
 }

 getch();
}
于 2013-11-11T17:52:49.980 回答