lil*_*tt8 2 c++ primes prime-factoring
我正在研究大数的素数分解(主要是项目3 @项目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
Run Code Online (Sandbox Code Playgroud)
不要尝试用0修改,它是未定义的!这样做会导致被零除错误.
long x = 0;
modulus = number % x; // x is 0 here and thus not valid
Run Code Online (Sandbox Code Playgroud)
根据维基百科关于Modulo Operations的文章,对我的答案进行扩展
一个模数0是未定义在大多数系统中,虽然有些不定义它是一个.