Skip to content

Instantly share code, notes, and snippets.

@dmwit
Created September 28, 2019 03:15
Show Gist options
  • Save dmwit/a899b1cda8901bc7afe3a51e20dcee8b to your computer and use it in GitHub Desktop.
Save dmwit/a899b1cda8901bc7afe3a51e20dcee8b to your computer and use it in GitHub Desktop.
derangements :: Integer -> Integer
derangements n = id
. sum
. zipWith (*) signs
. scanl (*) 1
$ [n,n-1..1]
where
signs = drop (n .&. 1) (cycle [1,-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment