Created
April 16, 2015 07:48
-
-
Save luxbock/6ee01a0fd51367e14380 to your computer and use it in GitHub Desktop.
C
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
(defn C [n] | |
(cond | |
(one? n) 1 | |
(even? n) (C (div n 2)) | |
:else (C (add1 (times 3 n))))) | |
(defn C* [n] | |
(cond | |
(= 1 n) 1 | |
(clojure.core/even? n) (C (quot n 2)) | |
:else (C (inc (* n 3))))) | |
(defn try-with-C [f upto] | |
(map (fn [n] | |
(try (f n) | |
(catch Exception e "Exception"))) | |
(range 1 (inc upto)))) | |
(comment | |
;; Wow! | |
(time (try-with-C C 100)) ;; "Elapsed time: 0.072 msecs" | |
(time (try-with-C C* 100)) ;; "Elapsed time: 0.069 msecs" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment