You are "holding on to the head". Try replacing "def perms" with "defn perms []" and calling it when you start using it. That way the permutations are let go as soon as they come. I noticed this with some scripts that go through large files: it's really important to create lazy longs sequences just for the one use and let them be garbage collected right away.
Tuomas On Mar 9, 6:47 pm, Dirk Vleugels <dirk.vleug...@gmail.com> wrote: > I should re-test before post. This version of the main loop actually > terminates (for smaller numbers), but will still blow up: > > (loop [ p (first perms) r (rest perms) s 0 c 0 ] > (if (> c 8) > s > (if (every? #(%1 p) guards) > (do > (println p) > (recur (first r) (rest r) (+ s (seq-to-val p)) (inc c))) > (recur (first r) (rest r) s c)))) > > Regards, > Dirk > > > > On Mon, Mar 9, 2009 at 4:55 PM, Dirk Vleugels <dirk.vleug...@gmail.com> wrote: > > Hi, > > > $ time clojure euler43.clj > > (1 4 0 6 3 5 7 2 8 9) > > (1 4 3 0 9 5 2 8 6 7) > > (1 4 6 0 3 5 7 2 8 9) > > java.lang.OutOfMemoryError: Java heap space > > Dumping heap to java_pid7476.hprof ... > > > The hprof file is 330mb in size, i can't read it with jhat (even if i > > give it 1.5G ....). > > > I assume perms is the problem, but i dont know how to write this in a > > more memory efficient way - I'm not looking for a more efficient > > algorithm though.... > > > Thanx for any hint, > > Dirk > > > (use 'clojure.contrib.combinatorics) > > > (def perms (permutations (range 10))) > > > (defn seq-to-val [ s ] > > (reduce #(+ (* %1 10) %2) s)) > > > (defn extract [ s i ] > > (conj [] (nth s i) (nth s (inc i)) (nth s (inc (inc i))))) > > > (defn f24 [ s ] > > (= (mod (seq-to-val (extract s 1)) 2) 0)) > > > (defn f35 [ s ] > > (= (mod (seq-to-val (extract s 2)) 3) 0)) > > > (defn f46 [ s ] > > (= (mod (seq-to-val (extract s 3)) 5) 0)) > > > (defn f57 [ s ] > > (= (mod (seq-to-val (extract s 4)) 7) 0)) > > > (defn f68 [ s ] > > (= (mod (seq-to-val (extract s 5)) 11) 0)) > > > (defn f79 [ s ] > > (= (mod (seq-to-val (extract s 6)) 13) 0)) > > > (defn f810 [ s ] > > (= (mod (seq-to-val (extract s 7)) 17) 0)) > > > (def guards [ f24 f35 f46 f57 f68 f79 f810 ]) > > > (loop [ p (first perms) r (rest perms) sum 0 count 0 ] > > (when (= count 10) > > sum) > > (if (every? #(%1 p) guards) > > (do > > (println p) > > (recur (first r) (rest r) (+ sum (seq-to-val p)) (inc count))) > > (recur (first r) (rest r) sum (inc count)))) > > -- > Dirk Vleugels > 2scale GmbH --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---