Skip to content

Instantly share code, notes, and snippets.

@pepijndevos
Created January 29, 2011 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepijndevos/801804 to your computer and use it in GitHub Desktop.
Save pepijndevos/801804 to your computer and use it in GitHub Desktop.
(ns test)
; Two perfect logicians, S and P, are told that integers x and y have
; been chosen such that 1 < x < y and x+y < 100.
; S is given the value x+y and P is given the value xy.
; They then have the following conversation.
;
; P: I cannot determine the two numbers.
; S: I knew that.
; P: Now I can determine them.
; S: So can I.
(defn valid? [[x y]]
(< 1 x y (+ x y) 100))
; pairs of all number that satisfy the constraints
(let [pairs (filter valid? (for [x (range 100) y (range 100)] [x y]))]
; all products xy with more than on possible combination of x and y
(def product (map key (remove #(= (val %) 1) (frequencies (map (fn [[x y]] (* x y)) pairs)))))
; limit pairs to ones that produce a product from the above set
(def pairs (filter (fn [[x y]] ((set product) (* x y))) pairs)))
; all sums of x + y for the limited pairs
(def sum (distinct (map (fn [[x y]] (+ x y)) pairs)))
; get all possible pairs for the products
(defn options [op prs]
(map #(filter (fn [[x y]] (= % (op x y))) pairs) prs))
; likely pairs for P
(def P (filter #(= 2 (count %)) (options * product)))
; pairs for S
(def S (options + sum))
; sums where S has a single answer
(filter #(= 1 (count %)) S)
; (([2 6]) ([3 4]) ([4 6]))
; options for P given [4 6]
(filter #(= 24 (apply * %)) pairs)
; ([2 12] [3 8] [4 6])
; S has no way of figuring out x and y given [2 12] or [3 8]
(filter #(= 14 (apply + %)) pairs)
; ([2 12] [4 10] [5 9] [6 8])
(filter #(= 11 (apply + %)) pairs)
; ([2 9] [3 8] [4 7] [5 6])
;
;;;;;;;;;; [4 6] ;;;;;;;;;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment