我有这个简单的程序:
// Include libraries
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Include locals
// Start
#define NUMBER 600851475143
int main(int argc, const char* argv[])
{
long long int ans = 0;
long long int num = NUMBER;
vector<int> factors;
do
{
// Get lowest factor
for (int i = 1; i <= num; ++i)
{
if (!(num % i))
{
factors.push_back(i);
num /= i;
break;
}
}
} while (num > 1);
cout << "Calculated to 1.\n";
int highestFactor = numeric_limits<int>::min();
for (int i = 0; i < factors.size(); ++i)
{
if (factors[i] > highestFactor)
{
highestFactor = factors[i];
}
}
ans = highestFactor;
cout << ans << endl;
return EXIT_SUCCESS;
}
编译g++ -O2 -c -o prob3.o prob3.cpp
证明是成功的,但是当我运行它时,我什么也没看到,它一直在运行,最后我不得不 Ctrl-C (强制杀死)它。当我尝试添加
int main(int argc, const char* argv[])
{
cout << "Test\n";
到程序,Test
也没有打印出来。就像我的程序根本没有执行一样。
任何帮助或建议表示赞赏!
解决方案
我忘记了从 2 开始的素数。更改for (int i = 1
为for (int i = 2
.