My version follows the same algorithm (and so runs in the same amount of time), just arranged differently:
(defn corner-nums [n] (for [i (range 4)] (- (* n n) (* i (dec n))))) (defn sum-all-corner-nums [max-n] (let [ns (range 3 (inc max-n) 2) all-corner-nums (mapcat corner-nums ns)] (apply + 1 all-corner-nums))) Tidbits of interest: * I suck at reading nested code, so I move all but the most trivial lambdas into separate functions, and use (let) liberally to name things. Consequently, my code tends to be more verbose, but I think it makes life easier when revisiting code. * (range) takes an optional step size argument, so generating all odd numbers from 3 to max-n (inclusive) can be done via (range 3 (inc max- n) 2). * (+ 1 (reduce + collection)) can also be written as (apply + 1 collection). * (mapcat) performs a map, then concatenates the results into a single list. I find this quite handy. * It's a purely subjective thing, but I like using (inc x) and (dec x) when incrementing and decrementing x by 1, respectively. On Feb 14, 11:13 pm, Andreas Kostler <andreas.koestler.le...@gmail.com> wrote: > Hi all, > Does anyone wanna have a look at my solution for Project Euler Problem 28? > > (defn diagonal-sum [n-max] > (+ 1 (reduce + > (map (fn[n] > (reduce + (map #(- (* n n) (* % (- n 1))) (range 4)))) > (take-nth 2 (range 3 (+ 2 n-max))))))) > > The function does the job. The solution takes about 1.5msec on my machine to > compute. > I'd like to discuss more performant and/or more idiomatic solutions to that > problem :) > > The parts I'm not quite happy with are the take-nth and range constructs with > all the magic numbers in there... > > Cheers > Andreas > > -- > "Programs must be written for people to read, and only incidentally for > machines to execute." > - Abelson & Sussman, SICP > -- > ********************************************************** > Andreas Koestler, Software Engineer > Leica Geosystems Pty Ltd > 270 Gladstone Road, Dutton Park QLD 4102 > Main: +61 7 3891 9772 Direct: +61 7 3117 8808 > Fax: +61 7 3891 9336 > Email: andreas.koest...@leica-geosystems.com > > ************www.leica-geosystems.com************* > > when it has to be right, Leica Geosystems > > Please consider the environment before printing this email. -- 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