Use this page to post your fastest/optimal tools. Do not overwrite a tool, without first benching it against your own. This ensures that this page only contains the fastest/most accurate tools available.


(defn int-to-digits
  [n]
  (for [character (str n)]
    (Integer/parseInt (str character))))
 
(defn pandigital?
  [n]
  (let [digits (int-to-digits n)]
    (= (sort digits)
       (take (count digits) #{1 2 3 4 5 6 7 8 9}))))
 
-inerte


For improved readability:
(defn prime?
  "Tests whether a given number is prime."
  [n]
  (cond
    (or (= n 2) (= n 3))          true
    (or (divisible? n 2) (< n 2)) false
    :else                         (let [sqrt-n (Math/sqrt n)]
                                    (loop [i 3]
                                      (cond
                                        (divisible? n i) false
                                        (< sqrt-n i)     true
                                        :else            (recur (+ i 2)))))))</span>
-Kotarak
Misc:
(defn combinations
  [& cs]
  (reduce (fn [vs c] (mapcat #(map conj vs (repeat %)) c)) [[]] cs))
 
(defn divisible?
  [n d]
  (zero? (rem n d)))
 
(defn prime?
  " Tests for primailty "
  [n]
  (if (or (= n 2) (= n 3))
   true
   (if (or
        (divisible? n 2)
        (< n 2))
     false
     (loop [i 3]
      (if (divisible? n i)
        false
        (if (> i (. Math (sqrt n)))
          true
          (recur (+ i 2))))))))
 
(def primes
     (lazy-cons 2 ((fn this[n]
                     (let [potential-divisors (take-while #(<= (* % %) n) primes)]
                       (if (some #(zero? (rem n %)) potential-divisors)
                         (recur (inc n))
                         (lazy-cons n (this (inc n)))))) 3)))
 
(defn pandigital?
  [n]
  (= (apply sorted-set (map #(Integer. (str %)) (str n)))
     (apply sorted-set (map #(Integer. %) (range 1 (inc (count (str n))))))))
 
(defn digits->int
  [ds]
  (reduce (+ (* 10 %1) %2) ds))
 



The fastest method I have for generating a lazy stream of all primes:
(ns primes)
 
(defn- wheel2357 [] (cycle [2 4 2 4 6 2 6 4 2 4 6 6 2 6 4 2 6 4 6 8 4 2 4 2 4 8
 6 4 6 2 4 6 2 6 6 4 2 4 6 2 6 4 2 4 2 10 2 10]))
 
(defn- spin [l n] (lazy-seq (cons n (spin (rest l) (+ n (first l))))))
 
(defn- insert-prime [p xs table]
  (update-in table [(* p p)] #(conj % (map (fn [n] (* n p)) xs))))
 
(defn- reinsert [table x table-x]
  (loop [m (dissoc table x), elems table-x]
    (if-let [elems (seq elems)]
      (let [elem (first elems)]
    (recur (update-in m [(first elem)] #(conj % (rest elem))) (rest elems)))
      m)))
 
(defn- adjust [x table]
  (let [nextTable (first table),
    n (nextTable 0)]
    (if (<= n x) (recur x (reinsert table n (nextTable 1)))
    table)))
 
(defn- sieve-helper [s table]
  (when-let [ss (seq s)]
    (let [x (first ss), xs (rest ss),
      nextTable (first table),
      nextComposite (nextTable 0)]
      (if (> nextComposite x)
    (lazy-seq (cons x (sieve-helper xs (insert-prime x xs table))))
    (recur xs (adjust x table))))))
 
(defn- sieve [s]
  (when-let [ss (seq s)]
    (let [x (first ss), xs (rest ss)]
      (cons x (sieve-helper xs (insert-prime x xs
                         (sorted-map)))))))
(defn primes
  "(primes) creates a lazy stream of all positive prime numbers"
  []
  (concat [2 3 5 7] (sieve (spin (wheel2357) 11))))
 
 
 
-- puzzler



This runs faster on my machine. Can anyone else confirm?
(defn lazy-primes-3 []
  (letfn [(append-under-key [h key val]
         (assoc h key (conj (h key) val)))
      (run-iterators [h n]
         (dissoc (reduce #(append-under-key %1 (+ n %2) %2) h (h n)) n))
      (next-prime [h n]
         (if (h n) ;; If h has any items, it is not prime
           (recur (run-iterators h n) (+ n 2))
           (cons n (lazy-seq
            (next-prime (append-under-key h (* n n) (+ n n)) (+ n 2))))))]
    (cons 2 (lazy-seq (next-prime {} 3)))))
 
-ivar



;; Take a power of large numbers. Math/Pow overflows, "replicate" method is too slow.
(defn pow[number expt] (.pow (bigint number) expt))
;; ----
1:6 user=> (time (pow 1000 1000))
"Elapsed time: 2.721785 msecs"
--Kunjan Kshetri


Instead of using the above pow function, you should just take advantage of the expt function in clojure.contrib.math which "does the right thing" with all of clojure's numerical types.
(use 'clojure.contrib.math)
(expt 1000 1000)