Skip to content

Instantly share code, notes, and snippets.

@Sophia-Gold
Created March 28, 2018 05:56
Show Gist options
  • Select an option

  • Save Sophia-Gold/c606b8fc5fd9552f97f7aa23b1e3ff18 to your computer and use it in GitHub Desktop.

Select an option

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
(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