在 C++ 中计算大数

Calculating with high numbers in C++

我正在尝试用 C++ 编写素数查找器。我有一个我很满意的正常运行的程序,但是当我尝试计算数十亿的素数时,我收到如下错误消息:

"Implicit conversion from 'long' to 'int' changes value from 10000000001 to 1410065409" .

即使在数十亿的情况下,它的运行速度也相当快,所以我想测试一下极限,但这个数字的变化阻止了它。

您需要使用 long 类型(可以容纳比 int 更大的数字)。 这是 int 溢出的示例:

int test1 = 10;
int test2 = test1 * 89000000;
cout << test2;

长整数

long test1 = 10;
long test2 = test1 * 89000000;
cout << test2;

结果是正确的。