gpt4 book ai didi

c++ - c++中long long的模运算

转载 作者:太空狗 更新时间:2023-10-29 23:47:18 25 4
gpt4 key购买 nike

我正在研究大数的质因数分解(主要是 project 3 @ project Euler 。我需要对声明为 long long 的数字使用模数。每次我尝试对这个巨大的数取模时,我都会得到一个浮点异常。任何帮助都会非常感谢。谢谢。

我已经通过 gdb 运行它,看看发生了什么。下面是我的代码。在这一点上这是非常粗略的逻辑。 请不要给我问题的答案。我很乐意接受帮助,使它变得更好,只是请不要给我直截了当的答案。谢谢:)

long factor(long number) {
string br = "\n\r";
long x = 0;
/*this modulus variable is an attempt
to move the answer into a long long container
to see if that solves my floating point exception,
it didn't*/
long long modulus;

while(x <= number) {
modulus = number % x;
if(modulus == 0) {
cout << number/x << br;
return factor(number/x);
}//if number % x
else {
return x;
}//else
x++;
}//while

}//factor

最佳答案

不要尝试修改 0,它是未定义的!这样做会导致被零除错误。

long x = 0;
modulus = number % x; // x is 0 here and thus not valid

根据 Wikipedia's article on Modulo Operations 扩展我的答案

a modulo 0 is undefined in the majority of systems, although some do define it to be a.

关于c++ - c++中long long的模运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6322779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com