{--
Copyright (c) 2014 Gorka Suárez García

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--}

{- ***************************************************************

    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. The first ten terms would
    be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

    Let us list the factors of the first seven triangle numbers:

    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.

    What is the value of the first triangle number to have over
    five hundred divisors?

*************************************************************** -}
module Problem0012 (main) where

-- Calculates the integer square root in a fast way.
squareRoot :: Integer -> Integer
squareRoot x = truncate $ sqrt $ (fromIntegral x :: Double)

-- Checks if the integral a is multiple of b.
multipleOf :: Integer -> Integer -> Bool
multipleOf a b = (mod a b) == 0

-- Checks if a number is prime or not.
isPrime :: Integer -> Bool
isPrime 2 = True
isPrime n = not $ or [multipleOf n x | x <- 2:[3,5..upperLimit]]
    where upperLimit = squareRoot n + 1

-- The list of prime numbers.
primesList = [x | x <- [2..], isPrime x]

-- Gets the prime factors of a number. The result is reversed.
getFactors :: Integer -> [Integer]
getFactors 1 = [1]
getFactors number = gf number primesList []
    where gf n (p:ps) rs =
              if p > n then
                  rs
              else if multipleOf n p then
                  gf (div n p) (p:ps) (p:rs)
              else
                  gf n ps rs

-- Groups a given sorted list of factors.
groupFactors :: [Integer] -> [[Integer]]
groupFactors [] = []
groupFactors lst = gf lst (head lst) [] []
    where gf [] v ac rs = rs ++ [ac]
          gf (x:xs) v ac rs =
              if x == v then
                  gf xs v (x:ac) rs
              else
                  gf xs x [x] (rs ++ [ac])

-- Calculates the number of divisors from a factors list.
divLenFromFactors :: [Integer] -> Integer
divLenFromFactors [] = 0
divLenFromFactors [x] = 1
divLenFromFactors lst = f $ groupFactors lst
    where f [] = 0
          f [x] = toInteger $ length x
          f (x:xs) = if x == [1] then 1 + next
                     else len * (1 + next) + next
              where (len, next) = (toInteger $ length x, f xs)

-- Finds the solution of the problem.
findTriangleValue :: Int -> Integer
findTriangleValue bottom = ftv 2 1
    where primes = take (bottom + 1) primesList
          ftv n v = if toInteger bottom < len then v
                    else ftv (n + 1) (v + n)
              where len = divLenFromFactors (1 : getFactors v)

-- The solution is: 76576500 = TN(12375)
main = do putStr "The value of the first triangle number to have "
          putStrLn $ "over 500 divisors is " ++ show result ++ "."
    where result = findTriangleValue 500