我有这部分代码应该找到给定范围内所有整数的 Collatz 序列,由用户输入定义。问题是在for
循环中,current_number
永远不会增加,或者在内部while
循环中,current_number != 1
永远不会失败。我错过了什么?
while (lower != 0 && upper != 0) {
cout << "Lower bound (integer): ";
cin >> lower;
cout << "Upper bound (integer): ";
cin >> upper;
if (lower == 0 || upper == 0)
return 0;
for (current_number = lower; current_number <= upper;
++current_number) {
//cout << current_number << endl;
counter = 0;
sequence = sequence + to_string(current_number) + ", ";
while (current_number != 1) {
if (current_number % 2 == 0) {
current_number = current_number / 2;
sequence = sequence + to_string(current_number) + ", ";
}
else {
current_number = current_number * 3 + 1;
sequence = sequence + to_string(current_number) + ", ";
}
cout << current_number << endl;
++counter;
}
//if (counter > longest) {
// longest = counter;
// the_longest_seed = current_number;
//}
}
cout << sequence << endl;
}