Created
March 28, 2018 05:56
-
-
Save Sophia-Gold/c606b8fc5fd9552f97f7aa23b1e3ff18 to your computer and use it in GitHub Desktop.
two ways to count the number of 1s in all partitions of a given integer
This file contains hidden or 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 ones-in-partitions | |
| "Number of 1s in all partitions of an integer n" | |
| [n] | |
| (-> (repeat (dec n) 1) | |
| (clojure.math.combinatorics/partitions) | |
| (count))) | |
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| (defn partitions | |
| "Partition function for positive integers." | |
| [] | |
| (letfn [(p [n] | |
| (cons 1 | |
| (lazy-seq | |
| (add-series (p (+ n 1)) | |
| (concat | |
| (repeat (- n 1) 0) (p n))))))] | |
| (cons 1 | |
| (p 1)))) | |
| (defn ones-in-partitions' | |
| "Number of 1s in all partitions of an integer n" | |
| [n] | |
| (->> (partitions) | |
| (take (dec n)) | |
| (reduce +'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment