On Sep 18, 10:46 pm, John Harrop <jharrop...@gmail.com> wrote:
> On Fri, Sep 18, 2009 at 4:39 PM, Tassilo Horn <tass...@member.fsf.org>wrote:
>
> > Although that doesn't really help, the normal `reduce' doesn't do
> > better.
>
> > (reduce + (take 1000000 (iterate inc 1)))  ; works
> > (reduce + (take 10000000 (iterate inc 1))) ; OutOfMemoryError
>
> Are you sure? I'd expect that with
>
> (def integers (iterate inc 1))
>
> (reduce + (take 10000000 integers))
>
> but not with what you wrote. If it does happen with exactly what you wrote
> then clojure.core/reduce is holding onto the head, or you had a funny
> substitute reduce in whatever namespace you evaluated those in.

(use 'clojure.contrib.math)

(defn geeky-fact [n]
  (=
    (reduce + (map #(expt % 3) (take n (iterate inc 1))))
    (expt (reduce + (take n (iterate inc 1))) 2)))

Seems to handle large 'n':s OK, so reduce should be fine.

As a side note, the thing that started it all was this Tweet from
Joshua Bloch:

"Geeky fact of the day: the sum of the first n cubes equals the square
of the sum of the first n integers"
http://twitter.com/joshbloch/statuses/4080292481

Again, thank you all for your replies. The Clojure community is really
one of the friendliest I have encountered so far.

/Patrik

--~--~---------~--~----~------------~-------~--~----~
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
Note that posts from new members are moderated - please be patient with your 
first post.
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to