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):
(fn [n] (or (= 0 (mod n 5)) (= 0 (mod n 3)) ) )
(iterate inc 1)))
(defn sums [coll n]
(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
(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))
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to firstname.lastname@example.org
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at