#!/usr/bin/newlisp ;; Copyright (c) 2006, Sarken LLC ;; Contact: Gordon Fischer [ fischer@sarken.org ] ;; Released under the Creative Commons Attribution License ;; http://creativecommons.org/licenses/by/2.5/ (println "How many lines?") (set 'lines (int (read-line))) ;; The following code starts at zero not one ;; http://www.turtle.dds.nl/newlisp/pascal.lsp ;; So uncomment this for comparisons ;; (inc 'lines) ;; Let's make a triangular matrix of depth lines ;; But let's not duplicate work instead of this ... ;; 1 ;; 1 1 ;; 1 2 1 ;; 1 3 3 1 ;; let's just store (because we know it's symmetric) ;; 1 ;; 1 ;; 1 2 ;; 1 3 (println (time (begin (define (even? x) (= 0 (% x 2))) (set 'tri (dup '() lines)) (define (cellval row col) (if (or (= col 0) (< row 2)) (push 1 tri row col) (push (+ (tri (- row 1) (- col 1)) (tri (- row 1) col) ) tri row col) )) (dotimes (x lines) (dotimes (y (+ 1 (floor (div x 2)))) (cellval x y) )) ;; now we have the basis of the triangle (set 'max-width (length (string (apply max (flat tri))))) ;; This allows us to have nice formatting (if (even? max-width) (set 'cell-padding 2) (set 'cell-padding 3) ) (set 'empty-cell (dup " " (+ max-width cell-padding))) (define (print-cell x) (format (string "%" (+ max-width cell-padding) "d") x)) (dotimes (x lines) (set 'tmp (map print-cell (tri x))) (if (even? x) ;; If it's even we indent and add 1/2 a cell for padding. (println (append (dup empty-cell (ceil (div (- lines x) 2))) (0 (/ (length empty-cell) 2) empty-cell) ;; This line formats the values of the triangle and dupes the symmetric numbers (join (append tmp (reverse (chop tmp)))))) (println (append (dup empty-cell (ceil (div (- lines x -1) 2))) (join (append tmp (reverse tmp))))) ) ) ) 1) " ms") (exit)