116 Prime Sandwich times out

102 views
Skip to first unread message

Jim Altieri

unread,
Oct 4, 2012, 1:17:05 PM10/4/12
to 4clo...@googlegroups.com
Hey folks,
I know my solution is probably not the best, but it executes on my machine pretty quickly. Am I doing anything obviously wrong?

(fn [x] (let [prime? (fn [n] (if (= 2 n)
                               true
                               (not-any? #(zero? (rem n %)) (concat '(2) (range 3 (+ 1 (int (Math/sqrt n))))))))
               nextprime (fn [n] (if (= 2 n)
                                  3
                                  (some #(if (prime? %) %) (iterate (partial + 2) (+ n 2)))))]
           (if (>= x 5)
             (if (prime? x)
               (let [prev (do (- x (- (nextprime x) x)))]
                 (and (prime? prev) (= (nextprime prev) x)))
               false)
             false)))

hak

unread,
Oct 5, 2012, 4:27:07 AM10/5/12
to 4clo...@googlegroups.com
java functions, like Math/sqrt will kill the performance on 4clojure
replace (+ 1 (int (Math/sqrt n))) with n and it will work
Reply all
Reply to author
Forward
0 new messages