Created
October 22, 2017 17:10
-
-
Save taylorwood/83be626fb04b14cc9ea2b4310a0aee18 to your computer and use it in GitHub Desktop.
Recursive function for integer partitions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))))))) |
Author
taylorwood
commented
Oct 22, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment