Created
August 23, 2011 05:57
-
-
Save swannodette/1164447 to your computer and use it in GitHub Desktop.
faster.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(set! *warn-on-reflection* true) | |
(set! *unchecked-math* true) | |
(defn shout [^java.util.ArrayList people ^long n ^long counter] | |
(cond | |
(= (.size people) 1) (.get people 0) | |
(zero? counter) (do | |
(.remove people 0) | |
(recur people n (inc counter))) | |
:else (let [counter (if (= counter (dec n)) | |
0 (inc counter)) | |
f (.get people 0)] | |
(.remove people 0) | |
(.add people f) | |
(recur people n counter)))) | |
(defn josephus [people nth] | |
(let [al (java.util.ArrayList.)] | |
(dotimes [x people] | |
(.add al (inc x))) | |
(shout al nth 0))) | |
(defn run-el-recur-al [] | |
(println (josephus 40 3)) | |
(time | |
(dotimes [_ 1000000] | |
(josephus 40 3)))) | |
(comment | |
(run-el-recur-al) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment