I'm trying to calculate the result of a formula. The formula is below in TeX, which generates this:
Running the haskell runs for about 20 minutes, and then runs out of memory and quits.
How can I do this?
\sum_{x=3}^{12} \left( \sum_{h=0}^3 \left[ \frac{(P(45,x-h)+P(3,h))!}{P(45,x-h)! \times P(3,h)!} \times 26^{h}\right] \right) |
import Data.List (foldl1') | |
-- Factorial function | |
fact n = foldl1' (*) [1..n] | |
-- Permutation function | |
n `p` 0 = 1 | |
n `p` 1 = n | |
n `p` r = foldl1' (*) [n-r+1..n] | |
-- Main function | |
unique :: Integer -> Integer -> Integer | |
unique c n = c * foldl1' (\acc x -> acc + | |
foldl1' (\acc h -> | |
let npxh = n `p` (x-h) | |
threeph = 3 `p` h | |
in acc + | |
fact(npxh + threeph) * 26 ^ h | |
`div` (fact npxh * fact threeph) | |
) [0..3] | |
) [3..12] | |
main :: IO () | |
--main = print $ unique 6 45 | |
main = print $ unique 2 3 |