euler/Problem12.hs

9 lines
475 B
Haskell

-- The sequence of triangle numbers is generated by adding the natural numbers.
-- So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. ...
-- What is the value of the first triangle number to have over five hundred divisors?
import Data.List
triangles = scanl1 (+) [1..] :: [Int]
divisors n = concat [ [m, q] | m <- takeWhile (\x -> x^2 <= n) [1..], let (q, r) = n `divMod` m, r == 0 ]
main = print $ head $ [ n | n <- triangles, length (divisors n) > 500 ]