我目前正在开发一个C ++程序,其最终目标是显示给定值的素数因子列表,最高限度为unsigned long int
。
它利用前1000个素数的试验除法,并在此后的每个值中使用费马的因子分解法。
一般来说,我会说这个程序对于我测试过的大多数数字都没问题,包括一些更大的数字。但是,我似乎遇到了一定数量的障碍。例如,输入素数 18446744073709551557 时,它会完全崩溃。对于其他较大的值,例如 246819835539726757 ,它会正确显示前五个素因子(7,11,37,131和557),但会错误计算最后两个因子,即18379和64601。 / p>
该功能的代码如下:
std::list < unsigned long int >primeFactors(unsigned long int n)
{
bool complete = false;
std::list<unsigned long int> factors;
unsigned long int ans1 = 0;
unsigned long int ans2 = 0;
int a;
int b;
// If n is 0 or 1, return a blank list
//
// This is run outside the while loop, as n will never be 0 or 1 after
// the initial calculation
if (n == 0 || n == 1)
{
complete = true;
return factors;
}
while (complete == false)
{
// if n is a prime number, add to the list and return
if (isPrime(n) == true)
{
factors.push_back(n);
complete = true;
return factors;
}
// if n is even (i.e. divisible by 2), add 2 and n/2 to the list and
// return
else if (divisibleByPrime(n) != 0)
{
unsigned long int divisor = divisibleByPrime(n);
ans1 = divisor;
ans2 = n / divisor;
if (isPrime(ans1) == true && isPrime(ans2) == true)
{
factors.push_back(ans1);
factors.push_back(ans2);
complete = true;
return factors;
}
else
{
if (isPrime(ans1) == true)
{
factors.push_back(ans1);
n = ans2;
}
else
{
factors.push_back(ans2);
n = ans1;
}
}
}
else
{
// a is the square root of n + a^2
unsigned long int i = 1;
float squareRoot;
std::stringstream ss;
std::string str;
bool isDouble = true;
bool answerFound = false;
unsigned long int x;
unsigned long int y;
while (answerFound == false)
{
isDouble = false;
squareRoot = (float)sqrt(n + (i * i));
ss << squareRoot;
str = ss.str();
for (char& i : str)
{
if (strchr(".", i) != NULL)
{
isDouble = true;
}
}
if (isDouble == true)
{
ss.str("");
i += 1;
}
else
{
answerFound = true;
}
}
x = (unsigned long int) squareRoot;
y = i;
ans1 = x + y;
ans2 = x - y;
if (isPrime(ans1) == true && isPrime(ans2) == true)
{
factors.push_back(ans1);
factors.push_back(ans2);
complete = true;
return factors;
}
else
{
if (isPrime(ans1))
{
factors.push_back(ans1);
n = ans2;
}
else
{
factors.push_back(ans2);
n = ans1;
}
}
}
}
我没有包含isPrime
或divisibleByPrime
等功能,因为它们似乎在隔离环境中都能正常工作。
编辑:我很欣赏某些变量在某些时候有点混乱,但我打算尽快纠正。