# HG changeset patch # User Robert McIntyre # Date 1326889137 25200 # Node ID c8e35134bf8e40e98a5fe7df481626b5598c02a9 # Parent c7df1ea6fd7187c328e23990a5d21f2ba58c570d working on qotd. diff -r c7df1ea6fd71 -r c8e35134bf8e src/rlm/function_utils.clj --- a/src/rlm/function_utils.clj Tue Dec 27 23:27:22 2011 -0700 +++ b/src/rlm/function_utils.clj Wed Jan 18 05:18:57 2012 -0700 @@ -80,6 +80,22 @@ (dorun (map future-cancel futures)) answer)))) +(defn mix-pred + "Takes any number of mathematically equal functions with + possibly different run-times and returns a function that + runs each in a separate thread, returns the result from + the first thread which finishes, and cancels the other threads." + {:author "Robert McIntyre"} + ([pred & functions] + (fn [& args] + (let [result (promise) + futures (doall (for [fun functions] + (let [answer (apply fun args)] + (if (pred answer) + (future (deliver result (apply fun args))))))) + answer @result] + (dorun (map future-cancel futures)) + answer)))) diff -r c7df1ea6fd71 -r c8e35134bf8e src/rlm/qotd.clj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/rlm/qotd.clj Wed Jan 18 05:18:57 2012 -0700 @@ -0,0 +1,80 @@ +(ns rlm.qotd) +(rlm.rlm-commands/help) + +;;;There is a pair of integers, 1 < m < n, whose sum is less +;;;than 100. + +(def pos (fn [a] + (filter (fn [[a b]] (< a b)) + (map #(vector % a) (range 1 (- 100 a)))))) + +(def p0 (reduce concat (map pos (range 2 99)))) + +;;; Person S knows their sum, but nothing else about them. +;;; Person P knows their product, but nothing else about +;;; them. + +;;; Now, Persons S and P know the above information, and each +;;; one knows that the other one knows it. They have the +;;; following conversation: + +;;; P: I can't figure out what the numbers are. + + + +;; Eliminate pairs with a unique product. + +(defn group-by + "Split coll into groups where the f maps each element in a + group to the same value in O(n*log(n)) time." + [f coll] + (partition-by f (sort-by f coll))) + +(defn unique-by + "Remove all elements a,b of coll that for which + (= (f a) (f b)) in O(n*log(n)) time." + [f coll] + (reduce + concat + (filter #(= (count %) 1) (group-by f coll)))) + +(defn multiple-by + "Keep all elements a,b, a!=b of coll for which + (= (f a) (f b)) in O(n*log(n)) time." + [f coll] + (reduce + concat + (filter #(> (count %) 1) (group-by f coll)))) + +(defn prod [[a b]] (* a b)) + +(def p1 (multiple-by prod p0)) + +;;; S: I was sure you couldn't. + +;; Each possible sum s has a set of possible pairs [a b] +;; where (= s (+ a b)). Partition p0 (since he *was* sure) +;; by sum, and keep those pairs that belong in partitions +;; where each pair in that partition is in p1. + +(defn sum [[a b]] (+ a b)) + +(def p2 + (reduce + concat + (filter + (partial every? (set p1)) + (group-by sum p0)))) + + +;;; P: Then I have. + +;; Keep those pairs that have a unique product out of the +;; ones that are left. + +(def p3 + (unique-by prod p2)) + + +;;; S: Then I have, too. + diff -r c7df1ea6fd71 -r c8e35134bf8e src/rlm/rlm_commands.clj --- a/src/rlm/rlm_commands.clj Tue Dec 27 23:27:22 2011 -0700 +++ b/src/rlm/rlm_commands.clj Wed Jan 18 05:18:57 2012 -0700 @@ -124,7 +124,9 @@ "/home/r/java/jdk6u30-docs/jdk/api") (clojure.java.javadoc/add-local-javadoc "/home/r/java/jdk6u30-docs/jre/api") - + (clojure.java.javadoc/add-local-javadoc + "/home/r/proj/jmeCapture/docs") + nil)