需要帮助查找Prime Emirp C++中的错误

Need help finding error in Prime Emirp C++

本文关键字:C++ 错误 Emirp Prime 帮助 查找      更新时间:2023-10-16

嘿,我一直在试图找出这段代码中的错误。我应该向用户询问一个正整数,然后在每行中找出第一个emirps 5。。。我在这一点上完全陷入了困境。。感谢

  #include <iostream>
   using namespace std;
  int isPrime(int value); //Prototyle for "prime number function"
  int reverse (int value2); //Prototype for "emirp function"
  int main()
 {
//Ask the user for a positive number
cout << "Please enter a positive number: ";
int n;
cin >> n;
//Reject negative value input
if ( n < 1)
{
    cout << "INVALID NUMBER n";
}
else
    //Calculate all emirps up to 'n'.
    for (int test = 0; test < n; test++)
    {
        int number = 2;
        if (isPrime(number))
        {
            cout << "n" << reverse(number) << "ttt";
        }
    }
return 0;
  }
  int isPrime(int value)
 {
//If value is prime, the remainder (count) will be zero twice--for 1 and itself.
int divisor = 1;
int count = 0;
int prime = 0;
if (value % divisor == 0)
{
    count++;
    ++divisor;
}
if ((count = 2))
{
    int prime = value; //store prime value in new variable
}
return prime;
}

int reverse(int value2)
{
//reverse the number
value2*=10;
value2 = value2 %10;
value2/=10;
//same procedure as prime function
int divisor2 = 1;
int count2 = 0;
int emirp = 0;
if (value2 % divisor2 == 0)
{//if
        count2++;
        ++divisor2;
    }
    if ((count2 = 2))
    {
        int emirp = value2;
    }
return emirp;
system ("pause");

请花一些时间正确描述您的问题。我的灵力告诉我,用户输入一个数字,然后程序会打印所有素数,直到这个数字,数字颠倒。(一些赌客选择将数字颠倒的素数称为Emirp。)

嘿,我一直在努力找出这段代码中的错误。。。

嘿,这里没有一个错误。代码中错误百出!

  #include <iostream>
   using namespace std;
  int isPrime(int value);
  int reverse (int value2);
  int main()
 {
cout << "Please enter a positive number: ";
int n;
cin >> n;
if ( n < 1)
{
    cout << "INVALID NUMBER n";
}
else
    //Calculate all emirps up to 'n'.
    for (int test = 0; test < n; test++)   ## No need to test 0 or 1 for primality
    {
        int number = 2;
        if (isPrime(number))   ## You always test whether 2 is a prime here
        {
            cout << "n" << reverse(number) << "ttt";
        }
    }
return 0;
  }
  int isPrime(int value)
 {
//If value is prime, the remainder (count) will be zero twice--for 1 and itself.
int divisor = 1;
int count = 0;
int prime = 0;       ## (A)
if (value % divisor == 0)
{
    count++;         ## This statelment will be executed at most once
                     ## You should count divisors in a loop
    ++divisor;       ## Here, divisor is incremented, but never used again
                     ## Also, if this were inside a loop, you should increment
                     ## the divisor unconsitionally. The condition affects
                     ## only the count.
}
if ((count = 2))     ## This will set count to 2 and always evaluate to true
{
    int prime = value;   ## This variable will shadow the "prime" variable 
                         ## decralered at (A). This variable will cease to exist
                         ## as soon as the block closes, i.e. immedietely.
                         ## You just want "prime = 1", without the "int", which
                         ## declares a new variable.
}
return prime;       ## This prime is tze one declared at (A) and will be 0.
}

int reverse(int value2)
{
value2*=10;
value2 = value2 %10;    ## The remainder of a division by 10 of a number that
                        ## you have just multiplied by 10 is 0, rendering pretty
                        ## much the rest of the function useless ...
value2/=10;
int divisor2 = 1;       ## ... which doesn't hurt, because the rest of the
                        ## function was useless to start with. Reversing the
                        ## digits of a number isn't at all like finding a prime.
int count2 = 0;
int emirp = 0;
if (value2 % divisor2 == 0)
{
        count2++;
        ++divisor2;
    }
    if ((count2 = 2))
    {
        int emirp = value2;
    }
return emirp;
system ("pause");      ## First, this statement comes directly after a terurn, 
                       ## so it will never be reached. Second, if anywhere, it
                       ## should go into the main routine. You don't want the 
                       ## user to press a key before printing each number.

}

请学习:

  • 如何使用调试器逐步完成prigram,以了解te变量是如何变化的,以及程序实际做了什么
  • 循环和范围块(大括号)的工作方式
  • 何时声明新变量,何时使用现有变量;(你想要后者的次数会比你想象的要多)
  • 为了更好地组织程序,它将帮助您发现逻辑错误

至于你手头的问题:有足够的资源来测试素数和反转SO上的数字,这应该不难找到。