re: Challenge: find 'Kaprekar numbers' VIEW POST


I have much sympathy for LISP and Clojure but have not that much experience with it. So, to stay in touch with it, here is my Clojure program, mostly a one-to-one translation of my latest Haskell program:

(ns kaprekar.core
(use '[clojure.math.numeric-tower :as math])

(defn isKaprekar?
  [base n]
  (let [sqr (* n n)]
     (map #(math/expt base %1) (range))          ; list of divisors
     (drop-while #(<= %1 n))                     ; discard if too small
     (take-while #(< %1 sqr))                    ; discard if too big
     (some #(= 0 (mod (- sqr n) (- %1 1)))))))   ; remainder = 0? <=> kaprekar

(defn findKaprekar
  [base n]
   (range n)
   (filter #(isKaprekar? base %1))
   (cons 1))) 

(defn -main
  "Finding the Kaprekar number base 10 up to 10000000"
  [& args]
  (time (print (findKaprekar 10 (math/expt 10 8)))))

I really like the threading macros. :-) Computing up to 10^8 , the execution time of the jar file is ten times the execution time of the compiled Haskell. Did I make a serious mistake, performance-wise? Of course, there is the JVM, but I didn't expect this program to be slower with factor 10.

Code of Conduct Report abuse