picking the nth element from a particular series, (sum the numbers factored

either by 3 or 5 below 1000). One forum comment on the very first puzzle

raised the criticism that it's easy to solve the question as presented

(i.e. below 1000), but what if you wanted the sum below 10^18?

I started working towards the big number goal using lazy sequences.

First, a lazy sequence of numbers that are factored by 3 or 5. Second,

another lazy sequence such that the nth element is the sum of the first n

elements of the input sequence. Finally, pick the nth element, (it doesn't

quite match the question, but it's of similar complexity):

(def seq-3s-n-5s

(filter

(fn [n] (or (= 0 (mod n 5)) (= 0 (mod n 3)) ) )

(iterate inc 1)))

(defn sums [coll n]

(lazy-seq

(when-let [s (seq coll)]

(let [x (+ n (first s))]

(cons x (sums (rest s) x))))))

(nth (sums seq-3s-n-5s 0) (Math/pow 10 6))

However some where between (Math/pow 10 6) and (Math/pow 10 7) It throws an

error: java.lang.OutOfMemoryError: Java heap space. I'm wondering if I'm

crossing some threshold into a different number type? Or perhaps I've

missed some point and got the lazy sequence stuff wrong?

So my question has two parts:

(1) How can I fix this code so that it will run to further into the

sequence?

(2) What actually has gone wrong? How could you debug something like this?

Notes: we can trust that (nth seq N) is not the problem, it returns in

reasonable time for a test like this: (nth (iterate inc 1) (Math/pow 10 9))

Cheers,

Julian Kelsey.

--

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