c++ - 生成一个数的所有因数分解

标签 c++ c algorithm primes factorization

我想找到 [1,107] 范围内的所有数字除数。我知道它可以在 O(sqrt(n)) 中解决。但是在此之前必须运行 Eratosthenes 筛法,可以很容易地对其进行修改以获得数字的质因数分解(通过跟踪每个数字的质因数之一)。所以我想知道使用素因数分解生成所有因子会更有效吗?
令 n = p1k1 * p2k2 *....*pmkm

我认为这个记法经过筛选后可以在O(m+Σki)中得到。
经过一番思考,我想出了以下代码来生成因子:

int factors[]={2,5};        // array containing all the factors
int exponents[]={2,2};      // array containing all the exponents of factors
                            // exponents[i] = exponent of factors[i]
vector <int> ans;           // vector to hold all possible factors

/*
*   stores all possible factors in vector 'ans'
*   using factors and exponents from index l to r(both inclusive)
*/
void gen(int factors[],int exponents[],vector<int>& ans,int l,int r)
{
    if(l==r)                        
    {
        int temp = 1;
        for(int i=0;i<=exponents[l];i++)
        {
            ans.push_back(temp);
            temp *= factors[l];
        }
        return;
    }
    gen(factors,exponents,ans,l+1,r);
    int temp=factors[l];
    int size = ans.size();
    for(int i=1;i<=exponents[l];i++)
    {
        for(int j=0;j<size;j++)
        {
            ans.push_back(ans[j]*temp);
        }
        temp *= factors[l];
    }
}

我认为它的时间复杂度至少是 Ω(no of factors) = Ω(∠(1+ki)).

所以我的问题是:
1) 以这种方式生成因子是否比通常(O(sqrt(n)) 循环方法)更快?
2)上面给出的代码可以优化吗?

最佳答案

第一个最明显的优化是预分配答案 vector 。您确切知道将有多少个因数(因为您已经将公式指定为 ‖(1+ki) )。

如果您自己管理堆栈而不是使用递归,您将获得最佳解决方案(每个因素只需要 1 次查找和 1 次乘法)。

是这样的吗?

int factors_count = 1;
for (int i = 0; i < r; ++i)
{
    factors_count *= 1+exponents[i];
}
ans.resize(factors_count);
ans[0] = 1;
int count = 1;
for (int stack_level = 0; stack_level < r; ++stack_level)
{
    const int count_so_far = count;
    const int prime = factors[stack_level];
    const int exponent = exponents[stack_level];
    int multiplier = 1;
    for (int j = 0; j < exponent; ++j)
    {
        multiplier *= prime;
        for (int i = 0; i < count_so_far; ++i)
        {
            ans[count++] = ans[i] * multiplier;
        }
    }
}

我什至没有尝试编译它,所以买者自负。

关于c++ - 生成一个数的所有因数分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33594064/

相关文章:

java - 某种算法的大O表示法?

c - 在给定邻接图和多次遍历的情况下优化方法以找到遍历最多的边

c++ - 在带有小数增量的 for 循环中使用 'double' 数据类型时出现问题

c++ - 如何使用 C++ 在结构中的堆内存中定义变量?

c - 如何找到数组中最大和最小数字的位置?

c - 如何使用 winpcap 分配内存以发送高性能的大型 pcap 文件(大小大于可用内存)?

C++ 标准库 - 我应该什么时候使用它,什么时候不应该使用它?

c++ - 打印 3D 指针数组的元素

c++ - 快速 vector 初始化 C++

c - C 中的 Zip 动态压缩库用于流式传输