{- 
  
  Euler #45 in Haskell

  """  
  Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

  Triangle 	  	Tn=n(n+1)/2 	  	1, 3, 6, 10, 15, ...
  Pentagonal 	  	Pn=n(3n−1)/2 	  	1, 5, 12, 22, 35, ...
  Hexagonal 	  	Hn=n(2n−1) 	  	1, 6, 15, 28, 45, ...

  It can be verified that T(285) = P(165) = H(143) = 40755.

  Find the next triangle number that is also pentagonal and hexagonal.
  """


  This program was created by Hakan Kjellerstrand (hakank@gmail.com)
  Also see my Haskell page: http://hakank.org/haskell/
  
  
-}

import Data.List
-- import HakankUtils

pent :: Int -> Int
pent(n) = n*(3*n-1) `div` 2

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

hex :: Int -> Int
hex(n)  = n*(2*n-1)

euler45a' :: Int -> Int -> Int -> Int
euler45a' p t h
   | pp == tt && tt == hh = pp
   | otherwise = euler45a' p2 t2 h2
   where
      pp = pent p
      tt = tri t
      hh = hex h
      -- next step
      t2 = t+1
      tt2 = tri t2
      p2 = if tt2 > pp then p+1 else p
      h2 = if pp > hh || tt2 > hh then h+1 else h
      

euler45a :: Int
euler45a = euler45a' 166 286 144

main :: IO ()
main = do
         print euler45a -- (0.19 secs, 94,665,024 bytes)
