Skip to content

Instantly share code, notes, and snippets.

@zolrath
Created February 23, 2012 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zolrath/1894847 to your computer and use it in GitHub Desktop.
Save zolrath/1894847 to your computer and use it in GitHub Desktop.
;; 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