<算法>除数问题算法>
给定约数的数量,我们必须找到第一个三角形数。
三角形数与自然数之和相同。
我采用了从2开始取素数并排列它们的方法,使生成的数与三角形数匹配。
例如,假设我们有 5 个约数。我将从 2 开始的素数 (2,3,5)
视为 N=p1^a1*p2*a2*p3^a3
。除数的数量为(a1+1)(a2+1)....
,这里2,3,5
可以取幂并排列。然后n^2+n=2k
(k是排列得到的值)。我检查 n 值是否为整数。
除此之外我还没有找到任何有效的算法,有没有人有更优化的算法?
Given a the number of divisors, we have to find the first triangle number.
A triangle number is same as sum of natural numbers.
I had adopted the method of taking prime numbers starting from 2 and permute them so that the number generated matches triangle number.
For example, suppose we are given 5 divisors. I take primes starting from 2 onwards (2,3,5)
as N=p1^a1*p2*a2*p3^a3
. Number of divisors are (a1+1)(a2+1)....
here 2,3,5
can take powers and permuted. Then n^2+n=2k
(k is the value got from permutation). I check n value to be Integer.
I have not found any efficient algo besides this, does any one has more optimal one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用反向方法。由于第 n 个三角形数可以通过 (n^2 + n)/2 找到,因此您可以迭代 n 并为每个数字计算其除数。一些优化:
这种方法似乎更直接、更优化。此外,它保证您会找到第一个三角形数字。
You can use reverse approach. Since n-th triangle number can be found as (n^2 + n)/2, you can just iterate n and for each number count its divisors. Some optimizations:
This approach seems to be a bit more straightforward and optimal. Moreover, it guarantees that you will find the first triangle number.