다음은  이차방정식 x^2 - x - 1  = 0 의 양의 근 즉 황금비율(golden ratio)을 구하는 Common Lisp 애플리케이션 소스이다. 황금비율을 구하는 비례방정식은   1 : x = x : (x+1) 이며, 이를 이차방정식으로 표현한 것이 x^2 - x - 1  = 0 이다.

See:  http://en.wikipedia.org/wiki/Golden_ratio

아래의 소스는 CLisp 으로 테스트되었다.


 

  1. ;;  Filename: testGoldenRatio.lsp
  2. ;;    황금률(즉, 이차방정식 x^2 - x - 1  = 0 의 양의 근)을 계산한다.
  3. ;;
  4. ;;   Execute: clisp testGoldenRatio.lsp
  5. ;;
  6. ;;      Date:  2013. 9. 1.
  7. (defun printUsing()
  8.     (format t "Using: lisp testGoldenRatio.lsp [-h|-help]~%")
  9.     (format t "This calculates the value of the golden ratio.~%")
  10. )
  11. (defun handle-zero-error(msg)
  12.     (catch 'handle-zero-error
  13.       (foprmat t msg)
  14.       (quit)))
  15. ;; 이차방정식 a x^2 + b x + c  = 0 의 근을 구한다.
  16. (defun findQuadraticRoot(a b c)
  17.     (if (zerop a)
  18.         (throw 'fn-a 'done))
  19.     (if (< (- (* b b) (* 4 a c)) 0.0)
  20.         (throw 'fn-a 'done))
  21.     (setf x1 (/ (+ (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2.0 a)))
  22.     (setf x2 (/ (- (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2.0 a)))
  23.     (list x1 x2)
  24. )
  25. ;; 실행 시작 지점
  26. (if (and (> (length ext:*args*) 0) (or (equal (nth 0 ext:*args*) "-h") (equal (nth 0 ext:*args*) "-help"))) (progn
  27.     (printUsing)
  28.     (quit) ))
  29. (setf values (findQuadraticRoot 1.0 (- 1.0) (- 1.0)))
  30. (setf x1 (nth 0 values))
  31. (setf x2 (nth 1 values))
  32. (if (>= x1 x2) (progn
  33.     (format t "The bigger root is ~F, ~%" x1)
  34.     (format t "and the less root is ~F.~%" x2) )
  35.        (progn
  36.     (format t "The bigger root is ~F, ~%" x2)
  37.     (format t "and the less root is ~F.~%" x1) )
  38. )



실행> clisp testGoldenRatio.lsp
The bigger root is 1.618034,
and the less root is -0.618034.



Posted by Scripter
,