Commits

Ivan Andrus committed 0bf390a

Did another problem
Beginning to update to new clojure version

Comments (0)

Files changed (1)

            (java.util Date Date)
            (java.util Calendar Calendar)
            (java.util GregorianCalendar)
-           (java.text DateFormat DateFormat)))
-
-;{{{ problems 1-50
+           (java.text DateFormat DateFormat))
+  ;; (:use [clojure.contrib [seq :only []]])
+  ;; (:use [clojure.contrib [seq-utils :only []]])
+  )
+
+;{{{ Problems 1-50
 
 ;{{{ problem  1 -- sum of multiples
 
           (map #(* x %)
                (range 999 (- x 1) -1))))
 
-(use 'clojure.contrib.seq-utils)
+;; (use 'clojure.contrib.seq-utils)
 (defn palindromes []
   (apply merge (map #(apply hash-set (palindromes-from %))
                  (range 999 100 -1))))
 ;}}}
 ;}}}
 
-;{{{ problem 51 -- prime replace gives prime *
+;{{{ problem 51 -- prime replace gives prime
 
 ;; By replacing the 1st digit of *57, it turns out that six of the
 ;; possible values: 157, 257, 457, 557, 757, and 857, are all prime.
 ;; necessarily adjacent digits) with the same digit, is part of an
 ;; eight prime value family.
 
+(defn is-k-member-family? [k n]
+  (let [possible-digits (if (= (first n) "*")
+                          (range 1 10)
+                          (range 10))
+        fam (filter isprime?
+                    (map #(apply int-concat %)
+                         (for [x possible-digits]
+                           (map #(if (= "*" %) x %) n))))]
+    (if (>= (count fam) k)
+      fam)))
+
+;; (is-k-member-family? 6 '("*" 5 7))
+;; (is-k-member-family? 6 '(2))
+;; (is-k-member-family? 7 '(5 6 "*" "*" 3))
+
+;; TODO
+(defn possible-substitutions
+  ([digits]
+     (filter
+      (fn [x] (some #(= "*" %) x))
+      (possible-substitutions digits nil)))
+  ([digits filter]
+     (if (<= (count digits) 1)
+        ;; base case
+        (if (or (nil? filter)
+                (= filter (first digits)))
+          (list '("*") digits)
+          (list digits))
+        ;; recursive case
+        (let [r (possible-substitutions (rest digits) filter)
+              f (first digits)]
+          (concat
+           ;; We omit if possible
+           (if (or (nil? filter)
+                   (= f filter)) ;; f == filter so it's okay in both situations
+             (map #(cons "*" %) (possible-substitutions (rest digits) f)))
+           ;; Those where we don't omit
+           (map #(cons (first digits) %) r))))))
+
+;; (possible-substitutions (to-digits 12332))
+;; (possible-substitutions (to-digits 1))
+
+(defn has-k-member-family? [k n]
+  (let [digits (to-digits n)]
+    (some #(is-k-member-family? k %)
+          (possible-substitutions digits))))
+
+;; (has-k-member-family? 7 56113)
+;; (has-k-member-family? 7 56003)
+;; (has-k-member-family? 6 157)
+
+(defn smallest-prime-k-family [k]
+  (first
+   (filter #(has-k-member-family? k %)
+           primes)))
+
+;; (has-k-member-family? 6 (smallest-prime-k-family 6)) (13 23 43 53 73 83)
+;; (has-k-member-family? 7 (smallest-prime-k-family 7)) (56003 56113 56333 56443 56663 56773 56993)
+;; (has-k-member-family? 8 (smallest-prime-k-family 8))  (121313 222323 323333 424343 525353 626363 828383 929393)
+
+;; (time (smallest-prime-k-family 8))
+;; Elapsed time: 17258.831 msecs
+;; 121313
+
+;; TODO: maybe I should compare the primes against each other instead...
 
 ;}}}
 ;{{{ problem 51 -- same digits for 2x, 3x, 4x, 5x, 6x
 
 ;; This one is from http://richhickey.github.com/clojure/clojure.core-api.html
 ;; I'm not sure why I don't have it
-(defmacro ->>
-  "Threads the expr through the forms. Inserts x as the
-  last item in the first form, making a list of it if it is not a
-  list already. If there are more forms, inserts the first form as the
-  last item in second form, etc."
-  ([x form] (if (seq? form)
-              (with-meta `(~(first form) ~@(next form)  ~x) (meta form))
-              (list form x)))
-  ([x form & more] `(->> (->> ~x ~form) ~@more)))
+;; (defmacro ->>
+;;   "Threads the expr through the forms. Inserts x as the
+;;   last item in the first form, making a list of it if it is not a
+;;   list already. If there are more forms, inserts the first form as the
+;;   last item in second form, etc."
+;;   ([x form] (if (seq? form)
+;;               (with-meta `(~(first form) ~@(next form)  ~x) (meta form))
+;;               (list form x)))
+;;   ([x form & more] `(->> (->> ~x ~form) ~@more)))
 
 
 ;}}}