I have the following code:
(defn params-correct-lucky-numbers
( [upto] (params-correct-lucky-numbers upto true))
( [upto check-max]
(let [max (* 10 1000 1000)]
(cond
(< upto 1) (do (printf "The parameter upto should at least be 1
(%d)\n" upto)
false)
(and check-max
(> upto max)) (do (printf "The parameter upto should be
below %d (%d)\n" (inc max) upto)
false)
:else true))))
(defn lucky-numbers-avl
"Lucky numbers from 1 up-to.
1 <= upto-value <= 10.000.000
http://en.wikipedia.org/wiki/Lucky_number"
; doc-string and pre-condition should match
params-correct-lucky-numbers
[upto]
{:pre [(params-correct-lucky-numbers upto false)]}
(if (= upto 1)
; for 1 the algorithm does not work, so return value manually
(list 1)
(loop [i 1
avl (apply avl/sorted-set (range 1 upto 2))]
(let [n (nth avl i nil)]
(if (and n (<= n (count avl)))
(recur (inc i)
(reduce (fn [sss m] (disj sss (nth avl m)))
avl
(range (dec n) (count avl) n)))
(sequence avl))))))
When I execute in the REPL:
(time (count (lucky-numbers-avl 1E8)))
Then after some time the Clojure process takes the complete CPU (all
cores). Twelve minutes later I get:
OutOfMemoryError Java heap space java.lang.Long.valueOf (Long.java:840)
In a way I understand what is happening here, but should Clojure not fail
sooner? Now it is about ten minutes busy with something that is not going
to work. (If it is possible to predict that it is not going to work.)
--
Cecil Westerhof
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.