# HG changeset patch # User Robert McIntyre # Date 1330606043 25200 # Node ID 12d1367cf1aa76c1f530fe06000be056ab7c1f08 # Parent c8e35134bf8e40e98a5fe7df481626b5598c02a9 updating various utilities diff -r c8e35134bf8e -r 12d1367cf1aa src/rlm/function_utils.clj --- a/src/rlm/function_utils.clj Wed Jan 18 05:18:57 2012 -0700 +++ b/src/rlm/function_utils.clj Thu Mar 01 05:47:23 2012 -0700 @@ -65,7 +65,7 @@ ) -(defn mix +(defn race "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 @@ -80,19 +80,30 @@ (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." +(defn race-pred + "Takes any number of mathematically equal functions with possibly + different run-times and returns a function that runs each in a + separate thread, and returns the first available result x for + which (pred x) returns true (or not-valid, if (pred x) returns + false on all the results). Cancels the other threads upon + returning early." {:author "Robert McIntyre"} - ([pred & functions] + ([pred not-valid & 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))))))) + latch (java.util.concurrent.CountDownLatch. + (count functions)) + failure-case (future (.await latch) + (deliver result not-valid)) + futures + (doall + (cons failure-case + (for [fun functions] + (future + (let [answer? (apply fun args)] + (if (pred answer?) + (deliver result answer?) + (.countDown latch))))))) answer @result] (dorun (map future-cancel futures)) answer)))) diff -r c8e35134bf8e -r 12d1367cf1aa src/rlm/qotd.clj --- a/src/rlm/qotd.clj Wed Jan 18 05:18:57 2012 -0700 +++ b/src/rlm/qotd.clj Thu Mar 01 05:47:23 2012 -0700 @@ -1,27 +1,28 @@ (ns rlm.qotd) -(rlm.rlm-commands/help) -;;;There is a pair of integers, 1 < m < n, whose sum is less -;;;than 100. +;;; 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)))))) +;; These constraints makes a triangular sort of pattern. +(defn generate-valid-numbers + [n] + (filter (fn [[a b]] (< a b)) + (map #(vector % n) (range 2 (- 150 n))))) -(def p0 (reduce concat (map pos (range 2 99)))) +(def squish (partial reduce concat)) + +(def p0 (squish (map generate-valid-numbers (range 2 149)))) ;;; 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: +;;; 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 @@ -34,17 +35,13 @@ "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)))) + (squish (filter #(= (count %) 1) (group-by f coll)))) (defn multiple-by - "Keep all elements a,b, a!=b of coll for which + "Keep all elements 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)))) + (squish (filter #(> (count %) 1) (group-by f coll)))) (defn prod [[a b]] (* a b)) @@ -53,15 +50,16 @@ ;;; 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. +;; where (= s (+ a b)). Partition p0 (since S *was* sure) by +;; sum, and keep those pairs that belong in partitions where +;; each pair in that partition is in p1. (since the only way +;; he could be *sure*, is if every possibility for a given +;; sum had an ambiguous product. (defn sum [[a b]] (+ a b)) (def p2 - (reduce - concat + (squish (filter (partial every? (set p1)) (group-by sum p0)))) @@ -78,3 +76,92 @@ ;;; S: Then I have, too. +;; Keep those pairs that have a unique sum out of the +;; ones that are left. + +(def p4 (unique-by sum p3)) + + +(defn solve [limit] + (let [generate-valid-numbers + (fn + [n] + (filter (fn [[a b]] (< a b)) + (map #(vector % n) + (range 2 (- limit n))))) + p0 + (squish (map generate-valid-numbers + (range 2 (dec limit)))) + p2 + (squish + (filter + (partial every? (set p1)) + (group-by sum p0))) + + p3 (unique-by prod p2)] + (unique-by sum p3))) + +;;; Dylan START! +(defn results [beez] + (let + [ + pairs + (for [m (range 2 beez) + n (range (inc m) beez)] + [m n]) + + singleton? (comp zero? dec count) + + sum (fn [[x y]] (+ x y)) + product (fn [[x y]] (* x y)) + + inverse-sum + (fn [s] + (filter #(= s (sum %)) pairs)) + + inverse-product + (fn [p] + (filter #(= p (product %)) pairs)) + ] + + + (->> + pairs + + (filter #(< (sum %) beez)) + + ;; P: I cannot find the numbers. + (remove (comp singleton? + inverse-product + product)) + set + + ;; S: I was sure you couldn't. + ((fn [coll] + (filter + (comp (partial every? coll) + inverse-sum + sum) + coll))) + set + + ;;P: Then I have. + ((fn [coll] + (filter + (comp singleton? + (partial filter coll) + inverse-product + product) + coll))) + set + + ;;S: Then I have, too. + ((fn [coll] + (filter + (comp singleton? + (partial filter coll) + inverse-sum + sum) + coll))) + + ))) \ No newline at end of file diff -r c8e35134bf8e -r 12d1367cf1aa src/rlm/rlm_commands.clj --- a/src/rlm/rlm_commands.clj Wed Jan 18 05:18:57 2012 -0700 +++ b/src/rlm/rlm_commands.clj Thu Mar 01 05:47:23 2012 -0700 @@ -95,7 +95,7 @@ [] (use '[rlm - [function-utils :only [mix defmix runonce]] + [function-utils :only [race race-pred defmix]] [rlm-commands :only [undef ns-reset ns-nuke reload keymap-clojure keymap-normal rlm javadoc]] [ns-rlm :only [ns-clear ns-clone ls]] @@ -104,7 +104,7 @@ [shell-write :only [sw]] [classpath-utils :only [classpath add-to-classpath]] [dreams :only [megadef silence]] - [map-utils :only [map-keys map-vals filter-keys filter-vals]] + [map-utils :only [map-keys filter-keys filter-vals]] [visualize :only [visual]] [identify :only [identify]]] '[abomination.no-parens :only [quit]] @@ -126,6 +126,9 @@ "/home/r/java/jdk6u30-docs/jre/api") (clojure.java.javadoc/add-local-javadoc "/home/r/proj/jmeCapture/docs") + (clojure.java.javadoc/add-local-javadoc + "/home/r/java/tritonus.sourceforge.net/apidoc") + nil) diff -r c8e35134bf8e -r 12d1367cf1aa src/rlm/visualize.clj --- a/src/rlm/visualize.clj Wed Jan 18 05:18:57 2012 -0700 +++ b/src/rlm/visualize.clj Thu Mar 01 05:47:23 2012 -0700 @@ -19,8 +19,7 @@ (defmethod visual ImagePlus [image] - (.show image) - image) + (.show image) image) (defmethod visual (class 4) [color]