You are viewing a single comment's thread. Return to all comments →
O(n) space and O(n^2) time solution in Common Lisp, without using factorials
(defun next-pascal-row (row) (labels ;; accumulate in reverse ((iter (rem accum) (if (null (cdr rem)) (cons 1 accum) (iter (cdr rem) (cons (+ (car rem) (cadr rem)) accum))))) (iter row (list 1)))) (defun print-pascal (n-rows) (labels ((iter (i row) (format t "~{~a~^ ~}~%" row) (if (< i n-rows) (iter (1+ i) (next-pascal-row row))))) (iter 1 (list 1)))) (print-pascal (read))
Seems like cookies are disabled on this browser, please enable them to open this website
Pascal's Triangle
You are viewing a single comment's thread. Return to all comments →
O(n) space and O(n^2) time solution in Common Lisp, without using factorials