Mercurial > rlm
view src/rlm/qotd.clj @ 4:12d1367cf1aa
updating various utilities
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 01 Mar 2012 05:47:23 -0700 |
parents | c8e35134bf8e |
children |
line wrap: on
line source
1 (ns rlm.qotd)3 ;;; There is a pair of integers, 1 < m < n, whose sum is4 ;;; less than 100.6 ;; These constraints makes a triangular sort of pattern.7 (defn generate-valid-numbers8 [n]9 (filter (fn [[a b]] (< a b))10 (map #(vector % n) (range 2 (- 150 n)))))12 (def squish (partial reduce concat))14 (def p0 (squish (map generate-valid-numbers (range 2 149))))16 ;;; Person S knows their sum, but nothing else about them.17 ;;; Person P knows their product, but nothing else about18 ;;; them.20 ;;; Now, Persons S and P know the above information, and21 ;;; each one knows that the other one knows it. They have22 ;;; the following conversation:24 ;;; P: I can't figure out what the numbers are.26 ;; Eliminate pairs with a unique product.28 (defn group-by29 "Split coll into groups where the f maps each element in a30 group to the same value in O(n*log(n)) time."31 [f coll]32 (partition-by f (sort-by f coll)))34 (defn unique-by35 "Remove all elements a,b of coll that for which36 (= (f a) (f b)) in O(n*log(n)) time."37 [f coll]38 (squish (filter #(= (count %) 1) (group-by f coll))))40 (defn multiple-by41 "Keep all elements a,b of coll for which42 (= (f a) (f b)) in O(n*log(n)) time."43 [f coll]44 (squish (filter #(> (count %) 1) (group-by f coll))))46 (defn prod [[a b]] (* a b))48 (def p1 (multiple-by prod p0))50 ;;; S: I was sure you couldn't.52 ;; Each possible sum s has a set of possible pairs [a b]53 ;; where (= s (+ a b)). Partition p0 (since S *was* sure) by54 ;; sum, and keep those pairs that belong in partitions where55 ;; each pair in that partition is in p1. (since the only way56 ;; he could be *sure*, is if every possibility for a given57 ;; sum had an ambiguous product.59 (defn sum [[a b]] (+ a b))61 (def p262 (squish63 (filter64 (partial every? (set p1))65 (group-by sum p0))))68 ;;; P: Then I have.70 ;; Keep those pairs that have a unique product out of the71 ;; ones that are left.73 (def p374 (unique-by prod p2))77 ;;; S: Then I have, too.79 ;; Keep those pairs that have a unique sum out of the80 ;; ones that are left.82 (def p4 (unique-by sum p3))85 (defn solve [limit]86 (let [generate-valid-numbers87 (fn88 [n]89 (filter (fn [[a b]] (< a b))90 (map #(vector % n)91 (range 2 (- limit n)))))92 p093 (squish (map generate-valid-numbers94 (range 2 (dec limit))))95 p296 (squish97 (filter98 (partial every? (set p1))99 (group-by sum p0)))101 p3 (unique-by prod p2)]102 (unique-by sum p3)))104 ;;; Dylan START!105 (defn results [beez]106 (let107 [108 pairs109 (for [m (range 2 beez)110 n (range (inc m) beez)]111 [m n])113 singleton? (comp zero? dec count)115 sum (fn [[x y]] (+ x y))116 product (fn [[x y]] (* x y))118 inverse-sum119 (fn [s]120 (filter #(= s (sum %)) pairs))122 inverse-product123 (fn [p]124 (filter #(= p (product %)) pairs))125 ]128 (->>129 pairs131 (filter #(< (sum %) beez))133 ;; P: I cannot find the numbers.134 (remove (comp singleton?135 inverse-product136 product))137 set139 ;; S: I was sure you couldn't.140 ((fn [coll]141 (filter142 (comp (partial every? coll)143 inverse-sum144 sum)145 coll)))146 set148 ;;P: Then I have.149 ((fn [coll]150 (filter151 (comp singleton?152 (partial filter coll)153 inverse-product154 product)155 coll)))156 set158 ;;S: Then I have, too.159 ((fn [coll]160 (filter161 (comp singleton?162 (partial filter coll)163 inverse-sum164 sum)165 coll)))167 )))