Quadratic equation in Lisp
Example for versions
Clojure 1.0.0,
Clojure 1.1.0
(defn solve-quadratic [a b c]
(if (= a 0)
"Not a quadratic equation."
(let [D (- (* b b) (* 4 a c))
k1 (- 0 b)
k2 (* 2 a)]
(if (= D 0)
(str "x = " (/ k1 k2))
(if (> D 0)
(let [k3 (Math/sqrt D)]
(str (str "x1 = " (/ (+ k1 k3) k2) (str "\nx2 = " (/ (- k1 k3) k2)))))
(let [k3 (Math/sqrt (- 0 D))]
(str (str (str (str "x1 = (" (/ k1 k2)) (str ", " (/ k3 k2))) ")\nx2 = (") (str (str (/ k1 k2) ", ") (str (- 0 (/ k3 k2)) ")") ))
))))))
(import '(java.util Scanner))
(def scan (Scanner. *in*))
(def a (.nextInt scan))
(def b (.nextInt scan))
(def c (.nextInt scan))
(println (solve-quadratic a b c))
Comments
]]>blog comments powered by Disqus
]]>