title: Efficiently finding the number of divisors in Haskell
body: <p>Trying to work out Problem 12 on Project Euler in Haskell.</p>
  
  <blockquote>
    <p>The sequence of triangle numbers is generated by adding the natural
    numbers.</p>
    
    <p>So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:</p>
  
  <pre><code>1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
  </code></pre>
    
    <p>Let us list the factors of the first seven triangle numbers:</p>
  
  <pre><code>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.
  </code></pre>
    
    <p>What is the value of the first triangle number to have over five
    hundred divisors?</p>
  </blockquote>
  
  <p>My solution works fine for small numbers of divisors (e.g. given 5, it returns 28), but when 500 is input, it seems to hang indefinitely.</p>
  
  <pre><code>-- Lazily generates infinite list of triangle numbers.
  triangleNumbers :: [Integer]
  triangleNumbers = map (\x -&gt; sum [1..x]) [1..]
  
  -- Given a number, returns the a tuple of how many divisors it has and the number.
  numDivisors :: Integer -&gt; (Int, Integer)
  numDivisors num = (length [x | x &lt;- [1..num], num `mod` x == 0], num)
  
  p12 :: Integer
  p12 = snd $ head $ filter (\x -&gt; fst x &gt; 500) $ map numDivisors triangleNumbers
  </code></pre>
  
  <p>Do you have any idea what I might be doing wrong? Thank you!</p>