One solution to euler problem 12

November 18, 2011, Tags: euler, haskell

Nothing special but play haskell for fun.

Priming is a simple module which can be found at 1

module Main where

import Data.List
import Primeing (primeFactors)

main :: IO ()
main = print $ p12 500

p12 :: Int -> Int
p12 n = head $ filter (factorLimit n) [ smartGaus x | x <- [1..]]

smartGaus :: Int -> Int
smartGaus n = (1+n)*n `div` 2

{-- |
  Is factor count under the limit  
--}
factorLimit :: Int -> Int -> Bool
factorLimit l n  
  | 2 * sqrtInt n < l    = False
  | otherwise            = length (factors  n) >= l


factors :: Int -> [Int]
factors n = concat [ [x, n `div` x] | x <- [1..sqrtInt n], n `mod` x == 0 ]

sqrtInt :: Int -> Int
sqrtInt = truncate . sqrt . fromIntegral