有效地得到一个定数的所有分频器分频器、定数、有效地

2023-09-11 03:26:56 作者:别让梦想只是梦想

根据该post,我们可以得到一些所有分频器通过以下codeS。

According to this post, we can get all divisors of a number through the following codes.

for (int i = 1; i <= num; ++i){
    if (num % i == 0)
        cout << i << endl;
}

例如,数的约数 24 1 2 3 4 6 8 12 24

在搜索一些相关的帖子以后,我没有找到什么好的解决办法。有没有做到这一点任何有效的方式?

After searching some related posts, I did not find any good solutions. Is there any efficient way to accomplish this?

我的解决办法:

找到给定数的所有素因子,通过这个的解决方案。 在获取这些主要因素的所有可能的组合。

然而,它似乎并没有成为一个很好的一个。

However, it doesn't seem to be a good one.

推荐答案

因素进行配对。 1 24 2 12 3 8 4 6

Factors are paired. 1 and 24, 2 and 12, 3 and 8, 4 and 6.

你的算法的改进可能是重复,以 NUM 的平方根,而不是一路 NUM ,然后用计算出成对的因素 NUM / I

An improvement of your algorithm could be to iterate to the square root of num instead of all the way to num, and then calculate the paired factors using num / i.