在给定长度无限循环的 Collat​​z 猜想中查找起始数的代码

Code to find starting number in Collatz Conjecture given length infinitely looping

我基本上在标题中已经说过了,但我正在尝试编写一个程序,它将为我提供最小长度的起始整数。我编写了一个不同的程序来计算给定起始整数的长度,但我在反转它时遇到了 很多 的麻烦,即使使用以前的代码作为基础也是如此。我输入的目标长度为 3,这应该会在 x = 2 处停止代码,但事实并非如此。相反,它在循环,但我不确定为什么。

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   char choice; //whether the program will run again

   do
   {
      int x = 1; //starting integer
      int targetK = 0; //desired minimum length
      int i = 0; //number of iterations run
      int lengthK = 0; //length of sequence

      cout << "\nEnter a required length." << endl;
      cin >> targetK;

      while (lengthK < targetK)
      {
         int lengthK = 1; //length of sequence

         do
         {
            cout << x << endl;

            if (x % 2 == 0)
            {
               x /= 2;
            }
            else
            {
               x = x * 3 + 1;
            }
            lengthK++;
         } while (x != 1);

         i++;
         x += i;
      }

      cout << x;
      cout << "\nLength of sequence before repeating: " << lengthK << endl;
      cout << "Would you like to run the function again? (Y/N)" << endl;
      cin >> choice;
   } while (choice == 'y' || choice == 'Y');

   return 0;
}

cout 只是让我知道它工作正常 -- 或者,在这种情况下,我怎么知道它不是。

所以,我发现您的代码存在四个问题。首先,您使用语句 int lengthK = 1; 在 while 循环中创建一个新的局部变量 lengthK。此变量与循环外部用于测试终止条件的变量同名,但它是不同的变量。由于除了在循环内递增它之外,您没有对值进行任何操作,所以我假设这是一个错误,并且您确实打算使用您首先声明的变量。这是无限循环的主要原因。

您只需说 lengthK = 1; 即可解决此问题。只需删除 int,您将不再声明一个生命周期仅限于循环的全新变量,而是更改现有变量的值。我实际上认为它可能应该是 lengthK = 0;,但这在很大程度上取决于其他细节。

我看到的第二个问题是你在做 x += i;。我很确定你只是想要 x = i;,尽管 x += i; 可能会意外工作,因为当它到达那段代码时 x 总是 1。 x = i;肯定会更清楚

第三个问题也很棘手,因为很难准确地知道你的意图。但我认为你想要一个 while 循环,测试在顶部,而不是内部循环的 do 循环。

最后一题比较简单,只影响输出的有用性,不影响算法的正确性。你可能想打印出 x - 1 而不是 x 因为实际上给你足够长的 Collat​​z 链的 x 的值比 x 退出循环时的值小一个.

我冒昧地编辑了您的问题以稍微加强您的代码。仔细缩进是一个非常好的习惯,我很高兴看到它。但是过多的白色 space 几乎和太少一样糟糕。

这是我修改过的代码版本,它可以解决您的问题。我在大多数地方删除了 endl,这样它会 运行 更快。

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   char choice; //whether the program will run again

   do
   {
      int x = 1; //starting integer
      int targetK = 0; //desired minimum length
      int i = 0; //number of iterations run
      int lengthK = 0; //length of sequence

      cout << "\nEnter a required length." << endl;
      cin >> targetK;

      do
      {
         ++i;
         x = i;
         lengthK = 1; //length of sequence

         cout << "Testing " << x << "\n  ";
         while (x != 1)
         {
            cout << x << " -> ";

            if (x % 2 == 0)
            {
               x /= 2;
            }
            else
            {
               x = x * 3 + 1;
            }
            lengthK++;
         }
         cout << "1\n";
      } while (lengthK < targetK);

      cout << i;
      cout << "\nLength of sequence before repeating: " << lengthK << endl;
      cout << "Would you like to run the function again? (Y/N)" << endl;
      cin >> choice;
   } while (choice == 'y' || choice == 'Y');

   return 0;
}