有效地得到一个定数的所有分频器 [英] Efficiently getting all divisors of a given number
问题描述
根据该<一href="http://stackoverflow.com/questions/11995069/finding-factors-of-a-number-not-getting-accurate-results">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
.
这篇关于有效地得到一个定数的所有分频器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!