Skip to content

Instantly share code, notes, and snippets.

@taylorwood
Created October 22, 2017 17:10
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 taylorwood/83be626fb04b14cc9ea2b4310a0aee18 to your computer and use it in GitHub Desktop.
Save taylorwood/83be626fb04b14cc9ea2b4310a0aee18 to your computer and use it in GitHub Desktop.
Recursive function for integer partitions
(defn parts
"Returns integer partitions for n."
([n]
(parts n n [] []))
([n m acc prefix]
(if (= 0 n)
(conj acc prefix)
(reduce
(fn [st i]
(into st (parts (- n i) i acc (conj prefix i))))
acc
(reverse (range 1 (inc (min m n))))))))
@taylorwood
Copy link
Author

(parts 5)
=> [[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment