我有一个小于 500,000,000 的数字,我想以一种有效的方式分解它。你建议什么算法?注意:我的时间限制是 0.01 秒!
我刚刚编写了这段 C++ 代码,但它非常糟糕!
void factorize(int x,vector<doubly> &factors)
{
for(int i=2;i<=x;i++)
{
if(x%i==0)
{
doubly a;
a.number=i;
a.power=0;
while(x%i==0)
{
a.power++;
x/=i;
}
factors.push_back(a);
}
}
}
加倍是这样的:
struct doubly
{
int number;
int power;
//and some functions!!
};
还有一点:我知道 n 不是素数