Created
November 15, 2011 11:46
-
-
Save condotti/1366883 to your computer and use it in GitHub Desktop.
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 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