Skip to content

Instantly share code, notes, and snippets.

@condotti
Created November 15, 2011 11:46
Show Gist options
  • Save condotti/1366883 to your computer and use it in GitHub Desktop.
Save condotti/1366883 to your computer and use it in GitHub Desktop.
(defn combination [k xs]
(if (pos? k)
(if (<= (count xs) k) (list xs)
(concat
(map #(conj % (first xs)) (combination (dec k) (rest xs)))
(combination k (rest xs))))))
(defn all-partitions [xs]
(map #(list % (seq (reduce disj (set xs) %)))
(combination (/ (count xs) 2) xs)))
(defn sum [xs] (apply + xs))
(defn sumofsquare [xs] (apply + (map #(* % %) xs)))
(defn sumofcube [xs] (apply + (map #(* % % %) xs)))
(defn phil-harvey
"Phil Harvey wants us to partition {1,…,16} into two sets of
equal size so each subset has the same sum, sum of squares
and sum of cubes."
[xs]
(first (drop-while #(or (not= (sum (first %)) (sum (second %)))
(not= (sumofsquare (first %)) (sumofsquare (second %)))
(not= (sumofcube (first %)) (sumofcube (second %))))
(all-partitions xs))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment