有效地找到Haskell中的除数的数量

时间:2015-08-23 20:37:03

标签: haskell number-theory

尝试在Haskell中的Project Euler上解决问题12。

  

通过添加自然生成三角数序列   号。

     

所以第7个三角形数字是1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.前十个术语是:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
     

让我们列出前七个三角形数字的因子:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
 We can see that 28 is the first triangle number to have over five divisors.
     

超过五个的第一个三角形数字的值是多少   一百个除数?

我的解决方案适用于少量除数(例如,给定5,它返回28),但是当输入500时,它似乎无限期地挂起。

-- Lazily generates infinite list of triangle numbers.
triangleNumbers :: [Integer]
triangleNumbers = map (\x -> sum [1..x]) [1..]

-- Given a number, returns the a tuple of how many divisors it has and the number.
numDivisors :: Integer -> (Int, Integer)
numDivisors num = (length [x | x <- [1..num], num `mod` x == 0], num)

p12 :: Integer
p12 = snd $ head $ filter (\x -> fst x > 500) $ map numDivisors triangleNumbers

你知道我可能做错了什么吗?谢谢!

2 个答案:

答案 0 :(得分:7)

另一个问题是你生成的三角形数字虽然正确,却效率很低。例如,要计算你正在求和的第11个数字[1..11],然后计算你正在求和的第12个数字[1..12],它不会使用先前计算的结果。

正如我在评论中提到的,您可以使用n*(n+1)/2直接计算第n个三角形数字。但是,即使您不知道这个公式,也可以通过使用这样的递归来利用连续三角数之间的相似性:

triangulars = go 1 2
  where go s n = s : go (s+n) (n+1)

这种递归也被scanl函数捕获:

triangulars = scanl (+) 1 [2..]

答案 1 :(得分:3)

问题是找到除数的数量的函数非常慢,因为它测试所有数字。有更高效的功能。例如,请参阅StackOverflow上此问题的答案:Two simple codes to generate divisors of a number. Why is the recursive one faster?

但如果你谷歌一点点,你可以找到许多其他算法。