4clojure 97 Pascal's Triangle

4 Jun 2012

4clojure problem 97 Pascal’s Triangle - calculate the nth row:

Pascal's Triangle

My solution:

(defn pascal [n]
  (if (= n 1)
    [1]
    (map #(apply + %)
      (partition 2 1
        (concat [0] (pascal (- n 1)) [0])))))

(pascal 5)
user=> (1 4 6 4 1)

A negative - my solution doesn’t use tail recursion; on my laptop it works up to about n = 760:

(pascal 10e4)
user=> java.lang.StackOverflowError (NO_SOURCE_FILE:0) 
comments powered by Disqus

  « Previous: Next: »