Skip to content

Instantly share code, notes, and snippets.

@ypsilon-takai
Created November 14, 2011 11:15
Show Gist options
  • Save ypsilon-takai/1363750 to your computer and use it in GitHub Desktop.
Save ypsilon-takai/1363750 to your computer and use it in GitHub Desktop.
project euler 60
(use 'clojure.contrib.math)
(defn concat-num [n m]
(let [zeros (first (drop-while #(<= % m) (iterate #(* 10 %) 1)))]
(+ (* n zeros) m)))
(defn prime-pair? [n m]
(and (is-prime? (concat-num n m))
(is-prime? (concat-num m n))))
(defn all-make-prime-pair? [p-set p]
(every? true? (map #(prime-pair? p %) p-set)))
(defn insert-pset [primeset-list pset]
(loop [h-list []
t-list primeset-list]
(cond (empty? t-list) (conj h-list pset)
(> (count pset)(count (first t-list))) (concat h-list [pset] t-list)
:else (recur (conj h-list (first t-list))
(next t-list)))))
(defn create-next [primeset-list pnum]
(filter #(all-make-prime-pair? (butlast %) (last %))
(map #(conj % pnum)
(conj primeset-list []))))
(defn pe60 [seq-count]
(loop [primeset-list []
primes (cons 3 (drop 3 (get-prime-list)))] ;; remove 2 and 5
(if (>= (count (first primeset-list)) seq-count)
(first primeset-list)
(let [nextset (create-next primeset-list (first primes))]
(recur (reduce insert-pset primeset-list nextset)
(next primes))))))
;; "Elapsed time: 965709.946376 msecs"
;; ouch!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment