다음 소스는 GNU CLisp 으로 실행됨을 확인하였다.

세 함수 pyra1(int int), pyra2(int int), pyra3(int int) 는 재귀호출을 사용하였고, 세 함수 pyramid1(), pyramid2(), pyramid3() 은 loop for 반복문을 사용하였다.

 

#!/usr/bin/env clisp

;;  Filename: pyramidOfDigits2.lsp
;;
;;   Execute: clisp pyramidOfDigits2.lsp
;;
;;     Or
;;
;;   Execute: ./pyramidOfDigits2.lsp
;;
;;     See: http://darvish.wordpress.com/2008/03/16/the-beauty-of-mathematics-and-the-love-of-god/
;;
;;  Date: 2013. 9. 23.
;;  Copyright (c) 2013 PH Kim  (pkim __AT__ scripts.pe.kr)


(setf *TEN*   10)
(setf *NINE*   9)
(setf *EIGHT*  8)


(defun pyra1(n i)
    (if (<= i *NINE*) (progn
 (format t "~9D x ~A + ~A = ~A~%" n *EIGHT* i (+ (* n *EIGHT*) i))
        (if (< i *NINE*)
      (pyra1 (+ (* n *TEN*) (+ i 1)) (+ i 1))))))

(defun pyra2(n i)
    (if (>= i 0) (progn
        (format t "~8D x ~A + ~A = ~A~%" n *NINE* i (+ (* n *NINE*) i))
        (if (> i 0)
      (pyra2 (+ (* n *TEN*) (+ i 1)) (- i 1))))))

(defun pyra3(n i)
    (if (<= i *TEN*) (progn
 (format t "~9D x ~A + ~2D = ~A~%" n *NINE* i (+ (* n *NINE*) i))
        (if (< i *TEN*)
      (pyra3 (+ (* n *TEN*) i) (+ i 1))))))

(defun pyramid1()
     (let ((i 0)
           (n 0)
           (s (make-list *TEN* :initial-element #\Space)))
 (loop for i from 1 below *TEN* do
  (setf (nth (- i 1) s) (code-char (+ 48 i)))
  (setf (nth i s)  #\Space)
  (setf n (parse-integer (list-to-string s)))
  (format t "~9D x ~A + ~A = ~A~%" n *EIGHT* i (+ (* n *EIGHT*) i)))
    ))

(defun pyramid2()
     (let ((i 0)
           (n 0)
           (s (make-list *TEN* :initial-element #\Space)))
 (loop for i from *NINE* downto 2 do
                ; (format t " i = ~A~%" i)
  (setf (nth (- *NINE* i) s) (code-char (+ 48 i)))
  (setf (nth (+ (- *NINE* i) 1) s) #\Space)
  (setf n (parse-integer (list-to-string s)))
  (format t "~8D x ~A + ~2A = ~A~%" n *NINE* (- i 2) (+ (* n *NINE*) (- i 2))))
    ))

(defun list-to-string (a)
    (format nil "~{~A~^~}" a))


(defun pyramid3()
     (let ((i 0)
           (n 0)
           (s (make-list *TEN* :initial-element #\Space)))
 (loop for i from 1 below *TEN* do
  (setf (nth (- i 1) s) (code-char (+ 48 i)))
  (setf (nth i s)  #\Space)
  (setf n (parse-integer (list-to-string s)))
  (format t "~9D x ~A + ~2D = ~A~%" n *NINE* (+ i 1) (+ (* n *NINE*) (+ i 1))))))


(format t "Use for loops~%")
(format t "Pyramid 1~%")
(pyramid1)
(format t "~%")

(format t "Pyramid 2~%")
(pyramid2)
(format t "~%")

(format t "Pyramid 3~%")
(pyramid3)
(format t "~%")

(format t "Use recursively called functions~%")
(format t "Pyramid 1~%")
(pyra1 1 1)
(format t "~%")

(format t "Pyramid 2~%")
(pyra2 9 7)
(format t "~%")

(format t "Pyramid 3~%")
(pyra3  1 2)
(format t "~%")


#|
Output:
Use for loops
Pyramid 1
        1 x 8 + 1 = 9
       12 x 8 + 2 = 98
      123 x 8 + 3 = 987
     1234 x 8 + 4 = 9876
    12345 x 8 + 5 = 98765
   123456 x 8 + 6 = 987654
  1234567 x 8 + 7 = 9876543
 12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321

Pyramid 2
       9 x 9 + 7  = 88
      98 x 9 + 6  = 888
     987 x 9 + 5  = 8888
    9876 x 9 + 4  = 88888
   98765 x 9 + 3  = 888888
  987654 x 9 + 2  = 8888888
 9876543 x 9 + 1  = 88888888
98765432 x 9 + 0  = 888888888

Pyramid 3
        1 x 9 +  2 = 11
       12 x 9 +  3 = 111
      123 x 9 +  4 = 1111
     1234 x 9 +  5 = 11111
    12345 x 9 +  6 = 111111
   123456 x 9 +  7 = 1111111
  1234567 x 9 +  8 = 11111111
 12345678 x 9 +  9 = 111111111
123456789 x 9 + 10 = 1111111111

Use recursively called functions
Pyramid 1
        1 x 8 + 1 = 9
       12 x 8 + 2 = 98
      123 x 8 + 3 = 987
     1234 x 8 + 4 = 9876
    12345 x 8 + 5 = 98765
   123456 x 8 + 6 = 987654
  1234567 x 8 + 7 = 9876543
 12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321

Pyramid 2
       9 x 9 + 7 = 88
      98 x 9 + 6 = 888
     987 x 9 + 5 = 8888
    9876 x 9 + 4 = 88888
   98765 x 9 + 3 = 888888
  987654 x 9 + 2 = 8888888
 9876543 x 9 + 1 = 88888888
98765432 x 9 + 0 = 888888888

Pyramid 3
        1 x 9 +  2 = 11
       12 x 9 +  3 = 111
      123 x 9 +  4 = 1111
     1234 x 9 +  5 = 11111
    12345 x 9 +  6 = 111111
   123456 x 9 +  7 = 1111111
  1234567 x 9 +  8 = 11111111
 12345678 x 9 +  9 = 111111111
123456789 x 9 + 10 = 1111111111
|#

 

 

Posted by Scripter
,