【发布时间】:2014-12-08 06:13:41
【问题描述】:
我一直在解决一个小问题,我需要将 18 位数字计算到它们各自的素数分解中。考虑到它确实有效,一切都可以编译并且运行得很好,但我希望减少素数分解的运行时间。我已经实现了递归和线程,但我认为我可能需要一些帮助来理解可能的大量计算算法。
每次我对预先制作的 4 个数字运行此操作时,大约需要 10 秒。如果有任何想法,我想将其减少到可能的 0.06 秒。
我注意到一些算法,例如 Sieve of Eratosthenes,并在计算之前生成了所有素数的列表。我只是想知道是否有人可以详细说明。例如,我在理解如何在我的程序中实施埃拉托色尼筛法或者它是否是一个好主意时遇到了问题。关于如何更好地解决这个问题的任何和所有指示都会非常有帮助!
这是我的代码:
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
using namespace std;
using namespace std::chrono;
vector<thread> threads;
vector<long long> inputVector;
bool developer = false;
vector<unsigned long long> factor_base;
vector<long long> primeVector;
class PrimeNumber
{
long long initValue; // the number being prime factored
vector<long long> factors; // all of the factor values
public:
void setInitValue(long long n)
{
initValue = n;
}
void addToVector(long long m)
{
factors.push_back(m);
}
void setVector(vector<long long> m)
{
factors = m;
}
long long getInitValue()
{
return initValue;
}
vector<long long> getVector()
{
return factors;
}
};
vector<PrimeNumber> primes;
// find primes recursively and have them returned in vectors
vector<long long> getPrimes(long long n, vector<long long> vec)
{
double sqrt_of_n = sqrt(n);
for (int i = 2; i <= sqrt_of_n; i++)
{
if (n % i == 0)
{
return vec.push_back(i), getPrimes(n / i, vec); //cause recursion
}
}
// pick up the last prime factorization number
vec.push_back(n);
//return the finished vector
return vec;
}
void getUserInput()
{
long long input = -1;
cout << "Enter all of the numbers to find their prime factors. Enter 0 to compute" << endl;
do
{
cin >> input;
if (input == 0)
{
break;
}
inputVector.push_back(input);
} while (input != 0);
}
int main()
{
vector<long long> temp1; // empty vector
vector<long long> result1; // temp vector
if (developer == false)
{
getUserInput();
}
else
{
cout << "developer mode active" << endl;
long long a1 = 771895004973090566;
long long b1 = 788380500764597944;
long long a2 = 100020000004324000;
long long b2 = 200023423420000000;
inputVector.push_back(a1);
inputVector.push_back(b2);
inputVector.push_back(b1);
inputVector.push_back(a2);
}
high_resolution_clock::time_point time1 = high_resolution_clock::now();
// give each thread a number to comput within the recursive function
for (int i = 0; i < inputVector.size(); i++)
{
PrimeNumber prime;
prime.setInitValue(inputVector.at(i));
threads.push_back(thread([&]{
prime.setVector(result1 = getPrimes(inputVector.at(i), temp1));
primes.push_back(prime);
}));
}
// allow all of the threads to join back together.
for (auto& th : threads)
{
cout << th.get_id() << endl;
th.join();
}
high_resolution_clock::time_point time2 = high_resolution_clock::now();
// print all of the information
for (int i = 0; i < primes.size(); i++)
{
vector<long long> temp = primes.at(i).getVector();
for (int m = 0; m < temp.size(); m++)
{
cout << temp.at(m) << " ";
}
cout << endl;
}
cout << endl;
// so the running time
auto duration = duration_cast<microseconds>(time2 - time1).count();
cout << "Duration: " << (duration / 1000000.0) << endl;
return 0;
}
【问题讨论】:
-
这个问题更适合Code Review
-
哦,拍哈哈,甚至不知道他们有一个关于这个的部分。谢谢!
-
当你到达 sqrt(n) 时停止寻找因子很好,但是当你递归时,你从 2 重新开始。如果没有小于
i的数字是n的因子,那么没有小于i的数字是n/i的因子,并且没有必要再次检查所有这些。特别是,如果i是n的最小因子并且i大于n的立方根,那么i和n/i是唯一的因子n。但是,没有必要进行特定的检查;如果您在i开始下一次搜索,它将是自动的。 -
嗯。您的第一个测试编号
a1 = 771895004973090566可以在不到 1/2000 秒(或更好)内计算出来,因为它是 2 x 385947502486545283。当然可以立即找到因子 2。然后,使用 Miller-Rabin 很容易确定 385947502486545283 是素数。类似地,a2 = 788380500764597944几乎可以立即分解为 2 x 2 x 2 x 7 x 14078223227939249。挑战实际上是分解硬半素数,例如 18436839306515468081 = 2988873347 x 6168491323,为此您需要 Shanks 的方型分解,Hart's因式分解,或 Brent-Pollard Rho。
标签: c++ multithreading algorithm primes prime-factoring