Last active
July 30, 2016 19:08
-
-
Save spchamp/053a293e07a90d1bddafa61b7b1f5d40 to your computer and use it in GitHub Desktop.
Factor ax^2 + bx + c in Common Lisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Author: Sean Champ, 30 July 2016 | |
;; License: Public Domain (#YMMV) | |
(defun grouping-m-n (a b c) | |
(let ((radic (sqrt (- (expt b 2) (* 4 a c)))) | |
(nb (- b))) | |
(values (/ (+ nb radic) -2) | |
(/ (- nb radic) -2)))) | |
(defun component-grouping (a b c) | |
(multiple-value-bind (m n) (grouping-m-n a b c) | |
(let* ((m_z (truncate m)) | |
(n_z (truncate n)) | |
(l (gcd a m_z)) | |
(d (gcd c n_z))) | |
(values | |
;; -- ax^2 + bx + c factored -- | |
;; for | |
;; n' = n_z / d | |
;; c' = c / d | |
;; rewrite ax^2 + bx + c | |
;; as (lx + d) + (n'x + c') | |
(list l d) | |
(list (/ n_z d) (/ c d)) | |
;; roots: x = -d/l, -c'/n' | |
(list (/ (- d) l) | |
(/ (- c) n_z)))))) | |
#| | |
Works Referenced | |
Calter, Paul A, and Michael A Calter. Technical Mathematics. 6th ed. Hoboken, N.J: John Wiley & Sons Inc, 2011. <https://www.safaribooksonline.com/library/view/technical-mathematics-sixth/9780470534922/> | |
|# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UPDATE: Avoid factoring on zero return values computed in
grouping-m-n