Created
February 23, 2012 20:15
-
-
Save zolrath/1894847 to your computer and use it in GitHub Desktop.
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
;; As it turns out, A is much slower than B in some cases, but faster in others! Fun. | |
(time (sum-divisorsA 2000000)) => "Elapsed time: 3766.863 msecs" | |
(time (sum-divisorsB 2000000)) => "Elapsed time: 113.786 msecs" | |
(time (sum-divisorsA 200000)) => "Elapsed time: 3.233 msecs" | |
(time (sum-divisorsB 200000)) => "Elapsed time: 8.436 msecs" | |
(time (is-abundant?A 1000000)) => "Elapsed time: 14.259 msecs" | |
(time (is-abundant?B 1000000)) => "Elapsed time: 58.678 msecs" | |
;; Methods marked A use the inefficient method, yet produce results faster. | |
;; Methods marked B use the MUCH faster sum-divisors method with no other changes but gets much slower as the range you sum rises. | |
(defn is-abundant?A [num] | |
(> (sum-divisorsA num) num)) | |
(defn is-abundantB? [num] | |
(> (sum-divisorsB num) num)) | |
(defn abundantlistA [start end] | |
(filter is-abundant?A (range start end))) | |
(defn abundantlistB [start end] | |
(filter is-abundant?B (range start end))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment