Permalink
Browse files

problem 41 in clojure.

  • Loading branch information...
1 parent 4f351fe commit b2f38faebe4ba1035ce18975a9555634b5789cf0 @jdunphy committed Jan 14, 2013
Showing with 49 additions and 5 deletions.
  1. +2 −0 .gitignore
  2. +39 −5 clojure/pandigital.clj
  3. +8 −0 project.clj
View
@@ -5,3 +5,5 @@ pom.xml
.lein-deps-sum
repl-port
clojure/target
+node_modules/
+target/
View
@@ -1,9 +1,43 @@
-(ns pandigital)
+(ns pandigital
+ (:require [clojure.math.numeric-tower :as math]
+ [clojure.math.combinatorics :as combo]))
+
+(def digits (set "123456789"))
(defn pandigital? [numbers]
"Returns true if input numbers are 1-9 pandigital. Input: list of numbers"
- (= (apply str (sort (apply str numbers))) "123456789"))
+ (let [num-str (apply str numbers)]
+ (and
+ (= 9 (.length num-str))
+ (= (set (apply str numbers)) digits))))
+
+(defn largest-pandigital-prime [start]
+ (loop [num start]
+ (println num)
+ (if (and (pandigital-num? num) (prime? num) )
+ num
+ (recur (- num 2)))))
+
+(defn pandigital-with-product? [a b] (pandigital? [a b (* a b)]))
+
+(defn vectors [i] (map #(vector i %) (range i 9000)))
+
+;; problem 32 solution
+(defn time-test []
+ (time (apply + (set (map #(apply * %) (filter #(apply pandigital-with-product? %) (mapcat #(vectors %) (range 100))))))))
+
+(defn pandigital-num? [number]
+ "Returns true if number is pandigital. Works for all numbers < 10 digits long."
+ (let [to-digits (set (str number)) num-digits (count (str number))]
+ (and (= (count to-digits) num-digits)
+ (= to-digits (set (take num-digits "123456789"))))))
+
+(defn prime? [n]
+ (let [divisors (concat [2 3] (range 5 (inc (math/ceil (math/sqrt n))) 2))]
+ (not-any? #(zero? (mod n %)) divisors)))
-(pandigital/pandigital? '(3245)) ;false
-(pandigital/pandigital? [123 45 67 98]) ;true
-(source pandigital/pandigital?)
+;; problem 41 solution
+;; largest n-digit pandigital prime
+(def largest-pandigital
+ (last (sort (filter prime?
+ (map #(Integer/parseInt (apply str %)) (combo/permutations (reverse (range 1 8))))))))
View
@@ -0,0 +1,8 @@
+(defproject euler "0.1.0-SNAPSHOT"
+ :description "solutions for project euler problems and 4clojure funtimes"
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.4.0"],
+ [org.clojure/math.numeric-tower "0.0.2"],
+ [org.clojure/math.combinatorics "0.0.3"]])

0 comments on commit b2f38fa

Please sign in to comment.