Skip to content

Instantly share code, notes, and snippets.

@callumacrae
Last active October 27, 2015 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callumacrae/5fd7dd85b71da4b3b969 to your computer and use it in GitHub Desktop.
Save callumacrae/5fd7dd85b71da4b3b969 to your computer and use it in GitHub Desktop.

I'm trying to calculate the result of a formula. The formula is below in TeX, which generates this:

the formula

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment